2

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?

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.