等待的承诺创建Array.map返回<pending>

我正在使用google-cloud-functions在将文件上传到Google云端存储时触发缩略图创建。

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]);
});

我希望最后一个console.log输出的是大小为4的数组,其中每个元素都包含getSignedurl()函数的回调。相反,我现在每次得到的是大小为4的数组,其中所有元素都是promise。

由于我已经在等待Promise.all(),所以我做错了什么才能导致这个问题?

haidimingzhushiwo 回答:等待的承诺创建Array.map返回<pending>

uploadPromises中尝试spreading Promise.all

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

问题在于uploadPromises已经是一个数组,并且您将其作为另一个数组的元素进行传递。通过使用spread syntax,可以将uploadPromises数组扩展为其元素。

本文链接:https://www.f2er.com/3067106.html

大家都在问