if (imageFile !== undefined) {
s3Url = await uploadImage(imageFile, user.user_id);
}
const uploadImage = async (attachment: IAttachment, userId: string) => {
if (!attachment) {
return '';
}
// attempting to upload image to S3
try {
await upload({
file: attachment.file,
fileName: attachment.fileName,
userId,
})
.then((s3Url) => {
return s3Url;
})
.catch((e) => {
return Promise.reject(e);
});
} catch (e) {
return Promise.reject(e);
}
};
唤醒结果,如下所示:
无论何时返回结果
响应:
答案 0 :(得分:1)
这是因为uploadImage
未返回任何内容:
// Promise is handled but not returned.
await upload({
file: attachment.file,
fileName: attachment.fileName,
userId,
})
.then((s3Url) => {
// This value is lost.
return s3Url;
})
.catch((e) => {
return Promise.reject(e);
});
首先,我将重构uploadImage
以仅使用async/await
:
const uploadImage = async (attachment: IAttachment, userId: string) => {
if (!attachment) {
return '';
}
// I removed try/catch block because the error was rethrown without modification.
const s3Url = await upload({
file: attachment.file,
fileName: attachment.fileName,
userId,
})
return s3Url;
};
现在您看到返回了一个值,并且您不再应该获得unedfined
。