Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use promises with AWS headObject?

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?

like image 751
GN. Avatar asked Jan 18 '26 02:01

GN.


2 Answers

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

like image 162
peteb Avatar answered Jan 20 '26 15:01

peteb


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.

like image 35
Samuel_NET Avatar answered Jan 20 '26 15:01

Samuel_NET



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!