admin管理员组文章数量:1289909
I'm in the process of replacing some old code that used jQuery Deferred objects and I am rewriting using Bluebird/ES6 Promises.
If I have multiple asynchronous calls, how can I trigger a function after all the promises are resolved.
Using jQuery Deferreds it would be something like this:
var requests = [...]; //some arbitrary data that is iterated to generate multiple ajax requests
var promises = [];
resuests.forEach(function(endpoint) {
promises.push($.ajax({url: endpoint}));
});
$.when.apply($, promises).then(function() {
alert('all promises plete!');
});
How do I rewrite this using ES6 Promise syntax?
I'm in the process of replacing some old code that used jQuery Deferred objects and I am rewriting using Bluebird/ES6 Promises.
If I have multiple asynchronous calls, how can I trigger a function after all the promises are resolved.
Using jQuery Deferreds it would be something like this:
var requests = [...]; //some arbitrary data that is iterated to generate multiple ajax requests
var promises = [];
resuests.forEach(function(endpoint) {
promises.push($.ajax({url: endpoint}));
});
$.when.apply($, promises).then(function() {
alert('all promises plete!');
});
How do I rewrite this using ES6 Promise syntax?
Share Improve this question asked Oct 29, 2014 at 15:40 DaveDave 10.9k3 gold badges44 silver badges54 bronze badges 1-
1
With jQuery promise syntax that could be
$.when.apply($, requests.map($.get)).then(...
- much cleaner – Benjamin Gruenbaum Commented Oct 29, 2014 at 15:52
2 Answers
Reset to default 6Using Promise.all
. Note that it takes an iterable such as an Array as its argument, unlike $.when
, so doesn't need the .apply
.
You'll also want to convert the jQuery Deferred to a native ES6 promise using Promise.resolve(thejQueryDeferred)
. EDIT: This gets done implicitly by the call to Promise.all
, so is really optional.
Whole code:
var requests = [...]; //some arbitrary data that is iterated to generate multiple ajax requests
var promises = [];
requests.forEach(function(endpoint) {
var nativePromise = Promise.resolve($.ajax({url: endpoint})); // if you want to make it clear that you're converting from jQuery Deferred to ES6 promise!
promises.push(nativePromise);
});
Promise.all(promises).then(function() {
alert('all promises plete!');
});
Since this is tagged bluebird in addition to the two good solutions you have already gotten here is a more "bluebird" way:
var requests = [...];
Promise.map(requests, $.get).then(function(results){
alert('all promises plete!');
});
This is probably as simple as it gets.
As the others have pointed out, the native es6 way would be to use Promise.all
, no Promise.resolve
or explicit creation is needed. The cleanest way with native promises would probably be:
var requests = [...];
Promise.all(requests.map($.get)).then(function(results){
});
本文标签: How to run after all javascript ES6 Promises are resolvedStack Overflow
版权声明:本文标题:How to run after all javascript ES6 Promises are resolved - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741474558a2380803.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论