How to use promises with AWS headObject?

∥☆過路亽.° 提交于 2019-12-23 01:52:57

问题


Using Node AWS SDK which supports callbacks and promises.. https://aws.amazon.com/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('complete', 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?


回答1:


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);




回答2:


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.



来源:https://stackoverflow.com/questions/42634779/how-to-use-promises-with-aws-headobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!