Answer the question
In order to leave comments, you need to log in
How to get value from interval?
var promise = new Promise((resolve, reject) => {
let intervalId = setInterval(function() {
let damage = getDamage(firstFighter, secondFighter);
if (damage > 0) {
console.log(secondFighterHealth + "-" + damage);
secondFighterHealth -= damage;
}else{
console.log('Второй блокировал удар первого!');
}
console.log('Second Health: ' + secondFighterHealth);
damage = getDamage(secondFighter, firstFighter);
if (damage > 0) {
console.log(firstFighterHealth + "-" + damage);
firstFighterHealth -= damage;
}else{
console.log('Первый блокировал удар второго!');
}
console.log('First Health: ' + firstFighterHealth);
if(firstFighterHealth <= 0) {
window.winner = secondFighter;
clearInterval(intervalId);
resolve(winner);
}else if(secondFighterHealth <= 0) {
window.winner = firstFighter;
clearInterval(intervalId);
resolve(winner);
}
}, 1000);
});
promise.then(
result => alert('Winner - ' + winner.name)
)
Answer the question
In order to leave comments, you need to log in
Read the documentation on promises, you have a lot of shit in your code.
Why async in your case, why await? Do you really understand how to use them?
My recommendation is to forget about them for a while and learn how to write working code on pure Promises, and only when you understand everything, you can try to return to async and await.
if (firstFighterHealth <= 0) {
winner = secondFighter;
clearInterval(intervalId);
resolve(winner);
// console.log(winner.name);
// return winner;
} else if (secondFighterHealth <= 0) {
let damage = getDamage(firstFighter, secondFighter);
if (damage > 0) {
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question