D
D
Dmitriy2020-05-14 10:32:15
JavaScript
Dmitriy, 2020-05-14 10:32:15

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)
    )

In the interval, by taking the life of each character, a winner is chosen.
It is necessary to return the object of the winner from the function.
Through promises does not work.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir, 2020-05-14
@Casufi

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) {

You win either the first player or the second, the third is not given, why else if? Use the normal else
let damage = getDamage(firstFighter, secondFighter);
if (damage > 0) {

Twice similar code, why take damage to a variable, why not write
if (getDamage(firstFighter, secondFighter) > 0)
if (getDamage(secondFighter, firstFighter) > 0)
Why not pass the current damage as the third parameter to the getDamage function, and remove the logic blocking into a common function, it is repeated, you can immediately return new damage
firstFighterHealth = getDamage(firstFighter, secondFighter, firstFighterHealth )
secondFighterHealth = getDamage(secondFighter, firstFighter, secondFighterHealth )
Before the interval, you need to do let firstFighterHealth = secondFighterHealth = 0;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question