0

I have an issue with Node Async Await and recursive functions. For some reason, the code execution stops after resolving the Promise for a recursive function. Below is just a simple example I put in to demonstrate my issue (though found the problem while sending some requests with requests library).

If you run the code, you will see

Starting
Inside Promise

but not "Test Completed".

function promiseFunc(number) {
    return new Promise((resolve, reject) => {
        if (number == 100) {
            console.log('Inside Promise');
            resolve('Done');
        } else {
            number += 1
            promiseFunc(number);
        }
    })
}


(async function testFunc() {
    console.log('Starting');
    await promiseFunc(0)
    console.log("Test completed");
})()

Could someone please advise what is the issue?

Thank you in advance.

3
  • 3
    A new Promise() runs 101 times, a resolve() runs once. See the problem? Commented Nov 6, 2019 at 11:18
  • Hmm..makes sense. Would you be able to help me convert my example to a right approach? Or at least point me to the right direction? Commented Nov 6, 2019 at 11:27
  • 1
    I do recommend you to read this article Promise Antipatterns You are probably having "The Collection Kerfuffle" ;) Commented Nov 6, 2019 at 11:31

2 Answers 2

2

Here are some ways to fix it:

function promiseFunc(number) {
    return new Promise((resolve, reject) => {
        if (number == 100) {
            console.log('Inside Promise');
            resolve('Done');
        } else {
            number += 1;
            resolve(promiseFunc(number));
        }
    })
}


(async function testFunc() {
    console.log('Starting');
    await promiseFunc(0)
    console.log("Test completed");
})()

Or, interestingly enough, the equivalent code that uses async/await instead of explicit new Promise(...), works for your case:

// an async function returns a Promise
async function promiseFunc(number) {
        if (number == 100) {
            console.log('Inside Promise');
            return 'Done';
        } else {
            number += 1;
            // it seems to work as intended without saying return promiseFunc(...)
            promiseFunc(number);
        }
    }


(async function testFunc() {
    console.log('Starting');
    await promiseFunc(0)
    console.log("Test completed");
})()
Sign up to request clarification or add additional context in comments.

Comments

-1

You have to do like this

  function promiseFunc(number) {
    return new Promise((resolve, reject) => {
        http.post('/api')
        .then((data) => {
            resolve(data);
        })
    })
}

    (async function testFunc() {
    console.log('Starting');
    const result = [];
    for (let index = 0; index < 100; index++) {
        result[index] = await promiseFunc(0);
    }
    console.log("Test completed");
})()

2 Comments

This was just an example to show my issue. The real life scenario I have is where I make an API call multiple times to wait for the needed response from the API, and then use the response further in the code.
Updated answer accoding to your requirement

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.