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

How to make a promise from setTimeout

Update (2017) Here in 2017, Promises are built into JavaScript, they were added by the ES2015 spec (polyfills are available for outdated environments like IE8-IE11). The syntax they went with uses a callback you pass into the Promise constructor (the Promise executor) which receives the functions for resolving/rejecting the promise as arguments. First, since async … 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

Difference between microtask and macrotask within an event loop context

One go-around of the event loop will have exactly one task being processed from the macrotask queue (this queue is simply called the task queue in the WHATWG specification). After this macrotask has finished, all available microtasks will be processed, namely within the same go-around cycle. While these microtasks are processed, they can queue even … Read more

Issue in returning data retrieved from DB queries called in the loop

Let’s start with the general rule for using promises: Every function that does something asynchronous must return a promise Which functions are these in your case? It’s getPrayerInCat, the forEach callback, and Prayer.find. Hm, Prayer.find doesn’t return a promise, and it’s a library function so we cannot modify it. Rule 2 comes into play: Create … Read more