1

I am using Angular2 for my application and I have to consume Amazon S3 javascript sdk for browser. I have the following function:

getBuckets() {
        var s3 = this.getS3();
        var params = {};
        s3.listBuckets(params, function(err, response) {
            if (err) {
                // What to return?
            }
            else {
                // What to return?
            }
        })
}

s3.listBuckets is the javascript API from Amazon for S3. It is expecting a call back function. But the caller of getBuckets is expecting a Promise. How should I change the above getBuckets(), such that the caller of getBuckets(), will look like:

getBuckets().then(
...
)

Thanks in advance.

UnderWood
  • 803
  • 3
  • 12
  • 23
  • With Angular 2 consider observables rather than promises – jonrsharpe Jun 18 '16 at 20:51
  • I don't think this question is a duplicate because the answer isn't necessarily "how to convert callbacks to promises" but is actually "upgrade the AWS SDK and you will get promises". – Mark B Jun 18 '16 at 22:00

4 Answers4

3

If you are using TypeScript you can do something like this to return a promise. I can imagine it would be around the same in the JavaScript style:

getBuckets() {
     return new Promise(function (resolve, reject) {
        var s3 = this.getS3();
        var params = {};
        s3.listBuckets(params, function(err, response) {
            if (err) {
                reject(err);
            }
            else {
                resolve(response);
            }
        })
     })
}
Poul Kruijt
  • 69,713
  • 12
  • 145
  • 149
1

The AWS SDK for JavaScript has supported promises since version 2.3.0 released March 31st. Here's the annoucement.

I suggest you update to the latest SDK version if you are using something earlier than 2.3.0, and then use promises instead of callbacks.

Mark B
  • 183,023
  • 24
  • 297
  • 295
0

You can use $q, documentatie here: https://docs.angularjs.org/api/ng/service/$q

That would look something like this:

getBuckets() {
    var deferred = $q.defer();
    var s3 = this.getS3();
    var params = {};
    s3.listBuckets(params, function(err, response) {
        if (err) {
            deferred.reject(err);
        }
        else {
            deferred.resolve(response);
        }
    })
    return deferred.promise;
}
PeterH
  • 801
  • 2
  • 7
  • 16
  • Oh darn I'm sorry no it isn't. I just checked the Angular2 docs and there seem to have been some major changes. I couldn't find $q in the Angular2 docs, but saw something about Observables, like @jonrsharpe mentioned. – PeterH Jun 19 '16 at 06:52
0

you can create your own promise like this:

var myPromiseFunction = function(iserr){

 var deferred = $q.defer();

 var myreturnvalue = 'somevalue';
 if(iserr === true){

   deferred.reject('some error');

 }else{

   deferred.resolve(myreturnvalue);

 }

 return deferred.promise;

}

And call it like this:

var throwerror = false;

//or var throwerror = true; if you want to create an error
myPromiseFunction(throwerror).then(function(res){

 console.log(res);

}).catch(function(err){

 console.log(err);

})

This way you can create promises (they don't even have to be async but it would not have any purpose for non-async operations.

user3791775
  • 426
  • 3
  • 5
  • Thanks for the comment. But is this Angular2? – UnderWood Jun 18 '16 at 21:15
  • No, this is the latest 1.*. I don't know whether they changed the promises. Just try the above code in a controller or something and you'll know. (don't forget to inject $q). You can resolve callbacks like PeterH showed. – user3791775 Jun 18 '16 at 21:27