Angular2: Returning Promise
This question already has an answer here:
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.
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);
}
})
})
}
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;
}
链接地址: http://www.djcxy.com/p/55520.html
上一篇: 如何实现这个承诺的方法
下一篇: Angular2:回归诺言