Javascript 等待承诺已创建Array.map返回<;待定>;
我使用谷歌云函数在上传文件到谷歌云存储时触发缩略图创建Javascript 等待承诺已创建Array.map返回<;待定>;,javascript,node.js,asynchronous,google-cloud-functions,es6-promise,Javascript,Node.js,Asynchronous,Google Cloud Functions,Es6 Promise,我使用谷歌云函数在上传文件到谷歌云存储时触发缩略图创建 exports.generateThumbnail = functions.storage.object().onFinalize(async (object) => { // Removed code not relevant to the problem // Download file from bucket. await file.download({destination: tempLoca
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的数组,其中所有元素都是
因为我已经在等待承诺了。all()我做错了什么,导致了这个问题?尝试承诺中的上传承诺。all
:
const results = await Promise.all([
...uploadPromises,
file.getSignedUrl(config),
]);
问题是uploadPromises
已经是一个数组,而您将它作为另一个数组的元素传递。通过使用,可以将uploadPromises
数组展开到其元素中