You have to wrap your imageUpload code inside promise and then pass the data to resolve callback that you want to return, and if there is some error you pass them in reject callback, throwing error in asynchronous task can give unexpected behaviour, so use reject callback there.
async function imageUpload() {
const params = {
Bucket: BUCKET_NAME,
Key: product.media.name,
Body: product.media
};
return new Promise((resolve, reject) => {
s3.upload(params, function (s3Err, data) {
if (s3Err) {
reject(s3Error);
}
console.log(`File uploaded successfully at ${data.Location}`) // successfully get data.Location here
resolve(data.Location);
});
});
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…