0

//i'm trying
let promise = allDoc.map((stage, index) =>  new Promise(async (resolve, reject) => {
     for(let i=0; i < stage.children.length; i++){
        let layerItem  = stage.children[i].children
        for(let key of layerItem){
            //key.toImage is a library, so it only supports callback.
            await key.toImage({
                async callback(img) {
                    key.attrs.img = img 
                    resolve(await addPhoto(key.attrs))
                }
            })
        }
     }
}))

//It should run 40 times, but only run and run 20 times
await Promise.all(promise).then(async result => {
    console.log(result)
})



async function addPhoto(params){
  const stored = await s3.upload(params).promise()
  return stored
}

How do I know when a callback is complete inside multiple loops? I tried promise.all, but promise.all gets executed before it completes.

Fiit
  • 11
  • 2
  • 1
    [Is it an anti-pattern to use async/await inside of a new Promise() constructor?](https://stackoverflow.com/q/43036229) – VLAZ Jul 15 '21 at 10:13
  • I just started js so I don't know. I deleted async await and tried it, but the result was the same – Fiit Jul 15 '21 at 10:20

0 Answers0