Skip to content
Advertisement

NodeJs push in array not working within the async function

Here is my code…

JavaScript

When I’m trying to access schedule.passenger inside the async function It’s working but when trying to access outside the async function it’s not working.

Advertisement

Answer

forEach(async function will fire bunch of async functions and once array iterations are complete, the execution will move to next code block without waiting for all the async functions to complete.

This can be taken care of by using map function and returning promises and wait for all those.

JavaScript

Also await schedule.passenger.push({ is not correct because Array.prototype.push() is not an async operation.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement