maus
maus

Reputation: 190

Awaited Promises created Array.map return <pending>

I'm using google-cloud-functions to trigger thumbnail creation on uploading a file to Google cloud storage.

exports.generateThumbnail = functions.storage.object().onFinalize(async (object) => {

     // Removed code not relevant to the problem

     // Download file from bucket.
     await file.download({destination: tempLocalFile});

     const uploadPromises = sizes.map(async size => {
        const tempLocalThumbFile = path.join(os.tmpdir(), thumbFilePath);

        // Generate a thumbnail using Sharp.
        await sharp(tempLocalFile).resize(width, height).toFile(tempLocalThumbFile);
        console.log('Thumbnail created at', tempLocalThumbFile);

        return thumbFile.getSignedUrl(config);
  });

    // Get the Signed URLs for the original image.
    const results = await Promise.all([
        uploadPromises,
        file.getSignedUrl(config),
    ]);

    console.log(results[0]);
});

What I would expect the last console.log to output is an array of size 4, where each element contains the callback of the getSignedurl() function. Instead what I now get each time, is an array of size 4 where all elements are promises.

Since I'm already awaiting the Promise.all() what have I done wrong to end up with this issue?

Upvotes: 2

Views: 120

Answers (1)

Alberto Trindade Tavares
Alberto Trindade Tavares

Reputation: 10366

Try spreading uploadPromises in the Promise.all:

const results = await Promise.all([
   ...uploadPromises,
   file.getSignedUrl(config),
]);

The issue is that uploadPromises is already an array, and you were passing it as an element of another array. By using the spread syntax, you expand uploadPromises array into its elements.

Upvotes: 5

Related Questions