最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use promises with AWS headObject? - Stack Overflow

programmeradmin1浏览0评论

Using Node AWS SDK which supports callbacks and promises.. /

Using q as promise library.. AWS.config.setPromisesDependency(q);

const headObjProm = this.s3Client.headObject(headParams).promise();

 headObjProm
   .then(ret => {
     //ret is promise..
   })

console logging ret shows..

(resolve, reject) {
   self.on('plete', function(resp) {
     if (resp.error) {
       reject(resp.error);
     } else {
       resolve(resp.data);
     }
  });

I was under impression ret would be data or error message? The documentation on AWS is all done in callback style. How to use this with promises?

Using Node AWS SDK which supports callbacks and promises.. https://aws.amazon./blogs/developer/support-for-promises-in-the-sdk/

Using q as promise library.. AWS.config.setPromisesDependency(q);

const headObjProm = this.s3Client.headObject(headParams).promise();

 headObjProm
   .then(ret => {
     //ret is promise..
   })

console logging ret shows..

(resolve, reject) {
   self.on('plete', function(resp) {
     if (resp.error) {
       reject(resp.error);
     } else {
       resolve(resp.data);
     }
  });

I was under impression ret would be data or error message? The documentation on AWS is all done in callback style. How to use this with promises?

Share Improve this question edited Jul 4, 2022 at 2:51 peteb 19.5k9 gold badges57 silver badges66 bronze badges asked Mar 6, 2017 at 20:18 GN.GN. 9,92914 gold badges72 silver badges141 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

When you're initializing the Q package as the Promise to use, you need to specify the Promise property from Q.

AWS.config.setPromisesDependency(require('Q').Promise);

Since const headObjProm = this.s3Client.headObject(headParams).promise(); is asynchronous, how about say you have this in an async function and use await like so:

`const resolveHeadObject = async()=> await s3Client.headObject(headParams).promise()`

I use the await/async syntax and it works for me.

Don't forget to add .Promise to your Q require as well, if that's necessary.

发布评论

评论列表(0)

  1. 暂无评论