【发布时间】:2020-03-15 23:47:13
【问题描述】:
我正在使用 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.all() 我做错了什么导致这个问题结束?
【问题讨论】:
标签: javascript node.js asynchronous google-cloud-functions es6-promise