How can I fetch an array of URLs with Promise.all?

Yes, Promise.all is the right approach, but you actually need it twice if you want to first fetch all urls and then get all texts from them (which again are promises for the body of the response). So you’d need to do Promise.all(urls.map(u=>fetch(u))).then(responses => Promise.all(responses.map(res => res.text())) ).then(texts => { … }) Your current code … Read more

How to dodge jQuery promises completely when chaining two async jQuery functions?

You can adopt either of two approaches … Convert then combine : var p1 = Promise.resolve($.getJSON(url_1, params_1)); // voila 1! var p2 = Promise.resolve($.getJSON(url_2, params_2)); // voila 2! var p3 = Promise.all([p1, p2]).then(…); Combine then convert : var p1 = $.getJSON(url_1, params_1); var p2 = $.getJSON(url_2, params_2); var p3 = Promise.resolve($.when(p1, p2)).then(…); // voila 1 … Read more

Handling errors in Promise.all

Promise.all is all or nothing. It resolves once all promises in the array resolve, or reject as soon as one of them rejects. In other words, it either resolves with an array of all resolved values, or rejects with a single error. Some libraries have something called Promise.when, which I understand would instead wait for … Read more

Promise.all: Order of resolved values

Shortly, the order is preserved. Following the spec you linked to, Promise.all(iterable) takes an iterable as a parameter and internally calls PerformPromiseAll(iterator, constructor, resultCapability) with it, where the latter loops over iterable using IteratorStep(iterator). Resolving is implemented via Promise.all() Resolve where each resolved promise has an internal [[Index]] slot, which marks the index of the … Read more

Problems inherent to jQuery $.Deferred (jQuery 1.x/2.x)

Update: jQuery 3.0 has fixed the problems outlined below. It is truly Promises/A+ compliant. Yes, jQuery promises have serious and inherent problems. That said, since the article was written jQuery made significant efforts to be more Promises/Aplus complaint and they now have a .then method that chains. So even in jQuery returnsPromise().then(a).then(b) for promise returning … Read more

JavaScript ES6 promise for loop

As you already hinted in your question, your code creates all promises synchronously. Instead they should only be created at the time the preceding one resolves. Secondly, each promise that is created with new Promise needs to be resolved with a call to resolve (or reject). This should be done when the timer expires. That … Read more

Wait until all promises complete even if some rejected

Update, you probably want to use the built-in native Promise.allSettled: Promise.allSettled([promise]).then(([result]) => { //reach here regardless // {status: “fulfilled”, value: 33} }); As a fun fact, this answer below was prior art in adding that method to the language :] Sure, you just need a reflect: const reflect = p => p.then(v => ({v, status: … Read more