0

I am trying to use AWS Javascript SDK for accessing S3. I am using the Promises with 'Q' library. However it is not working. I have set the Q dependency also.

var Q = require('q');
AWS.config.setPromisesDependency(Q);

Here's the code I have:

var listBucketPromise = s3.listBuckets().promise();
listBucketPromise.then(
    function (response) {
        console.log(" response = " + response );
    } ,
    function (error) {
        console.log(" error = " + error);
    }
);

This shows on console :

response = function (resolve, reject) {
      self.on('complete', function(resp) {
        if (resp.error) {
          reject(resp.error);
        } else {
          // define $response property so that it is not enumberable
          // this prevents circular reference errors when stringifying the JSON object
          resolve(Object.defineProperty(
            resp.data || {},
            '$response',
            {value: resp}
          ));
        }
      });
      self.runTo();
    }

I have a valid s3 client set correctly. The callback format works:

s3.listBuckets(function (err, data) {
    console.log(data);
});

Why is the promise code not working ?

4
  • 1
    What do you mean by "This shows on console", is that an error message? Commented Jun 11, 2017 at 12:13
  • 2
    Assuming that setPromisesDependency expects a promise constructor, try passing Q.Promise instead Commented Jun 11, 2017 at 12:14
  • I am testing the script via WebStorm terminal. That's what I meant by console. The code that prints that log is : console.log(" response = " + response ); Commented Jun 11, 2017 at 12:34
  • AWS.config.setPromisesDependency(Q.Promise); did make the difference.. I am able to get the response now.. Many thanks !. If you could post that as an answer, I will be happy to accept it.. :) Commented Jun 11, 2017 at 12:40

1 Answer 1

1

You need to pass a Promise constructor to setPromisesDependency. The Q function that you used does not expect a callback, when called with the typical promise executor callback it just returned a promise fulfilled with that function value.

You can use Q.Promise instead, which also is documented in this blog post's example:

// Use Q implementation of Promise
AWS.config.setPromisesDependency(require('Q').Promise);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.