I'm learning Node.js.
I have to call an async function work() inside my Promise.all() loop and it must be completed before moving on to statements that are after the Promise.all().
Currently, it reaches the FINISH statment before completing work().
What is the right way to make the code wait for work() function to complete?
const promise1 = Promise.resolve(1);
const promise2 = Promise.resolve(2);
const promise3 = Promise.resolve(3);
async function work() {
await new Promise((resolve, reject) => {
setTimeout(resolve, 2000, 'foo');
})
console.log('some work here')
}
async function main() {
await Promise.all([promise1, promise2, promise3]).then((values) => {
values.forEach(function(item) {
console.log(item)
work()
});
});
console.log('FINISH')
}
main()