wait for a promise of promises

Im trying to download a bucket from S3

async function downloadBucketRaw(buckt, prefix) {
  return s3.listObjectsV2({ Bucket: buckt, Prefix: prefix },
    (err, obj) => {
        if (err) return console.log(err);

        var promises = []

        obj.Contents.forEach(element => {
            if (element.Size > 0) {
                var p = downloadFileFromS3({ Bucket: buckt, Key: element.Key });
                promises.push(p)
            }
        });
        return promises
    });
  }
}

async function downloadFileFromS3(item) {

    var file = s3.getObject(item,
      (err, data) => {
        if (err) throw err;
    });

    file.createReadStream()
      .pipe(fs.createWriteStream(item));

    return file.promise()
}

In order to wait for all the downloads to happen I want to do something like:

Promise.all(downloadBucketRaw(buckt, prefix))

But it is not working, I am not sure about how to do a wait of a promise returning a list of promises

  • The files here is a list of all files returned from all the async calls: const files = await Promise.all(downloadBucketRaw(buckt, prefix)) The bottleneck here is the file that takes the longest to download, since they are all downloaded simultaneously.

    – 




  • 1

    Do not pass a callback if you want to use promises?!

    – 

  • How can I do it without a callback? Im new in nodeJs

    – 

Leave a Comment