I have a nodejs function processReviews(workflow)
that when called, is supposed to push multiple promises to an array promises[]
, then run them all with promises.all()
after the for loop.
async function examplePromiseFunc(){ return new Promise(async (resolve, reject) => { console.log("examplePromiseFunc() INSIDE ") resolve('done') }) } async function processReviews(workflow){ //get objects from s3 let allObjects = await getAllObjects(workflow); allObjects = allObjects.filter(obj => obj.Key.includes('output.json')) console.log(`found ${allObjects.length} .json files.`) const promises = []; for (let i = 0; i < allObjects.length; i++) { console.log('i=',i,' pushing to promises[]') promises.push( examplePromiseFunc() ) } const result = await Promise.all(promises) console.log(result); }
But when I run my code, the output looks like this:
found 697 .json files. i= 0 pushing to promises[] examplePromiseFunc() INSIDE i= 1 pushing to promises[] examplePromiseFunc() INSIDE i= 2 pushing to promises[] examplePromiseFunc() INSIDE i= 3 pushing to promises[] examplePromiseFunc() INSIDE ...
Which means that each time I push a promise to my promises[] array (promises.push( await examplePromiseFunc() )
) the function examplePromiseFunc()
is getting called instantly and is not waiting.
I want my function to only get called when I run await Promise.all(promises)
at the end, is there something I'm missing? are my async functions causing a problem? I've been reading up on javascript promises.all and this seems like a fine implementation.
没有评论:
发表评论