如何使用 AWS headObject 的承诺?
How to use promises with AWS headObject?
使用支持回调和承诺的节点 AWS SDK..
https://aws.amazon.com/blogs/developer/support-for-promises-in-the-sdk/
使用 q 作为 promise 库..
AWS.config.setPromisesDependency(q);
const headObjProm = this.s3Client.headObject(headParams).promise();
headObjProm
.then(ret => {
//ret is promise..
})
控制台日志记录 ret
显示..
(resolve, reject) {
self.on('complete', function(resp) {
if (resp.error) {
reject(resp.error);
} else {
resolve(resp.data);
}
});
我的印象是 ret
是数据还是错误信息?
AWS 上的文档都是以回调方式完成的。
如何将其与 promises 一起使用?
当您将 Q
包初始化为要使用的 Promise 时,您需要从 Q
.
指定 Promise
属性
AWS.config.setPromisesDependency(require('Q').Promise);
因为 const headObjProm = this.s3Client.headObject(headParams).promise();
是异步的,假设你在异步函数中有这个并像这样使用 await 怎么样:
`const resolveHeadObject = async()=> await s3Client.headObject(headParams).promise()`
我使用 await/async
语法,它对我有用。
别忘了将 .Promise
添加到您的 Q
要求中,如果有必要的话。
使用支持回调和承诺的节点 AWS SDK.. https://aws.amazon.com/blogs/developer/support-for-promises-in-the-sdk/
使用 q 作为 promise 库..
AWS.config.setPromisesDependency(q);
const headObjProm = this.s3Client.headObject(headParams).promise();
headObjProm
.then(ret => {
//ret is promise..
})
控制台日志记录 ret
显示..
(resolve, reject) {
self.on('complete', function(resp) {
if (resp.error) {
reject(resp.error);
} else {
resolve(resp.data);
}
});
我的印象是 ret
是数据还是错误信息?
AWS 上的文档都是以回调方式完成的。
如何将其与 promises 一起使用?
当您将 Q
包初始化为要使用的 Promise 时,您需要从 Q
.
Promise
属性
AWS.config.setPromisesDependency(require('Q').Promise);
因为 const headObjProm = this.s3Client.headObject(headParams).promise();
是异步的,假设你在异步函数中有这个并像这样使用 await 怎么样:
`const resolveHeadObject = async()=> await s3Client.headObject(headParams).promise()`
我使用 await/async
语法,它对我有用。
别忘了将 .Promise
添加到您的 Q
要求中,如果有必要的话。