1

I'm trying to run async map in my NodeJS server, but it is not waiting the map to finish.

let test = `Start`;

    const array1 = ['A', 'B'];
    array1.map( async (arr1) => {
        test += ' ' + arr1;

        // Just a async request for testing. Return a name
        const pData = await property.getPropTotalUnitsByCompany(73);
        pData.map( (propData) => {
            test += ' ' + propData.PropertyName;
        });
    });

    console.log(test);

The console output is "Start A B". It never grab the data from property.getPropTotalUnitsByCompany.

getPropTotalUnitsByCompany returns an array with a couple of properties name. It looks like the code is not waiting to process the async request.

1
  • You're right that, as written, that will not wait for the async functions to complete. You can have your async functions return a promise and then call Promise.all on the result of the map function. If you aren't returning values in your map function, though, then you're misusing it, and should use array.forEach instead. The purpose of map isn't to iterate over an array, but to create a new array derived from the source array. Commented Jun 16, 2020 at 17:33

2 Answers 2

1

You're right that, as written, that will not wait for the async functions to complete. You can have your async functions return a promise and then call Promise.all on the result of the map function, like so:

let test = `Start`;

const array1 = ['A', 'B'];

const promises = array1.map( async (arr1) => {
    test += ' ' + arr1;

    // Just a async request for testing. Return a name
    return property.getPropTotalUnitsByCompany(73).then(pData => {
        pData.map((propData) => {
            test += ' ' + propData.PropertyName;
        });
    });
});

await Promise.all(promises);

console.log(test);
Sign up to request clarification or add additional context in comments.

1 Comment

Oops, just realized I downvoted by accident - I've edited your answer in order to be able to unlock the voting. Your answer is correct ofc - my downvote is now retracted, sorry :)
0

Your assumption was correct, your code does not wait for the promises to resolve. Since your not really mapping something (you're not returing anything from your map and are rather pushing to the test variable), this can be simplified using a simple for .. of:

for (const element of array1) {
    test += ' ' + element;

    // Just a async request for testing. Return a name
    const pData = await property.getPropTotalUnitsByCompany(73);
    pData.forEach((propData) => {
        test += ' ' + propData.PropertyName;
    });
}

Here's a slightly adapted version of your code:

const someAsyncFunc = () => new Promise(resolve => setTimeout(() => resolve(["x", "y"]), 10));

(async () => {
    let test = `Start`;

    const array1 = ['A', 'B'];
    for (const element of array1) {
        test += ' ' + element;

        // Just a async request for testing. Return a name
        const pData = await someAsyncFunc();
        pData.forEach((propData) => {
            test += ' ' + propData;
        });
    }

    console.log(test); // prints "Start A x y B x y"
})()

Comments

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.