2

Consider this code :

async function callPromise ()
{
    let num = await promiseEvenOdd()
    console.log(num, "is odd")
}

function promiseEvenOdd()
{
    return new Promise((resolve, reject) => {
        let num = Math.floor(Math.random() * 2) + 1
        console.log("num", num)  
        if (num % 2)
        {
            console.log("odd")
            resolve(num)
        }
        else
        {
            console.log("even")
            return promiseEvenOdd()
        }

      })
}

callPromise ()

If the number is even we call a new promise, when the number is odd we resolve the promise, but after some even numbers, and after resolving, the await is never returned,

Any idea to how resolve it ?

Thanks,

Bigs

visconti
  • 45
  • 1
  • 5
  • 1
    Why are you even using promises here? There are no asynchronous operations. If you just want an odd random number, you can just create a loop and return the random number directly. – jfriend00 Jul 09 '18 at 18:04
  • 1
    Looking at your code in more detail, it's even harder to figure out what you're trying to do. `Math.floor(Math.random() * 2) + 1` makes a random number between `x` where `1 <= x < 3` so the only values it can make are `1` and `2` so the ONLY odd number in there is `1`. So, you're trying to randomly make a `1`? Doesn't make sense. – jfriend00 Jul 09 '18 at 18:12
  • Haha it is just an example about promises, not about random :-)) – visconti Jul 09 '18 at 19:38
  • CRice solved my problem, i think it is good to isolate when you're asking something, it is why i created this piece of fake code – visconti Jul 09 '18 at 19:42
  • 1
    Fake code does NOT get you the best answers here on stack overflow because when you don't show us your real code, we can't understand the whole problem and offer you the BEST way to solve the problem in ways you haven't even thought of to ask about. So, for the best help with things you didn't even know to ask about, we have to see your real code. – jfriend00 Jul 09 '18 at 20:25

1 Answers1

4

The issue is that you always have to call resolve or reject when you create a promise. In your even case, you return the result of promiseEvenOdd, but you never resolve the original promise, which is why it hangs forever.

To fix that, you should just resolve the original promise with the result of the recursive call, instead of returning it. Eg, change the line

return promiseEvenOdd()

to:

resolve(promiseEvenOdd())

That way, the first promise will resolve with the result of the next one.

Example:

async function callPromise ()
{
    let num = await promiseEvenOdd()
    console.log(num, "is odd")
}

function promiseEvenOdd()
{
    return new Promise((resolve, reject) => {
        let num = Math.floor(Math.random() * 2) + 1
        console.log("num", num)  
        if (num % 2)
        {
            console.log("odd")
            resolve(num)
        }
        else
        {
            console.log("even")
            resolve(promiseEvenOdd())
        }

      })
}

callPromise ()
CRice
  • 29,968
  • 4
  • 57
  • 70
  • Thanks a lot, but now i would like to resolve this promise inside a EventEmitter.on function, you think it is possible ? – visconti Jul 09 '18 at 18:11
  • It's certainly possible, but I can't give any specifics without seeing how you're trying to do it. Could you perhaps edit your question to include your attempt at using it that way? – CRice Jul 09 '18 at 18:18
  • Thanks for your help CRice, it works like a charm, i wish you a good day ! – visconti Jul 09 '18 at 19:15
  • This looks borderline to a [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it). – Bergi Jul 10 '18 at 12:52
  • This was the perfect solution for the problem that I was trying to solve. Thanks, CRice. – Inspector6 Jun 10 '19 at 00:27