【发布时间】:2019-12-19 08:56:45
【问题描述】:
promise() 在此处使用 AWS lambda。
我想在文件存储到 s3 后立即发送信号。我想知道 .promise() 在这里做了什么。 (--s3.putObject({}).promise()--)
fetch(send_to_this_url) 的时间戳在 s3 存储上看到文件时几乎相同。基于此,promise() 不会异步操作。我这里有什么问题吗?
在前几行中,响应正常时返回响应。但是,如果它早点返回,文件就不会存储在 s3 中,但它已经存储了,所以我想知道第一个 if(response.ok) 的目的是什么。
请与我分享任何想法。!
fetch(url)
.then((response) => {
if (response.ok) {
return response;
}
return Promise.reject(new Error(
`Failed to fetch ${response.url}: ${response.status} ${response.statusText}`));
})
.then(response => response.buffer())
.then((buffer) => {
s3.putObject({
ACL: "public-read",
Bucket: process.env.BUCKET,
Key: key,
Body: buffer
}).promise();
try{
const send_to_this_url = "someurl?key=" + key;
fetch(send_to_this_url);
}catch(e){
callback('error' + e.message);
}
}
)
.then(v => callback(null, v), callback);
【问题讨论】:
标签: javascript node.js lambda promise aws-sdk-js