When is the body of a Promise constructor callback executed?

Immediately, yes, by specification. From the MDN: The executor function is executed immediately by the Promise implementation, passing resolve and reject functions (the executor is called before the Promise constructor even returns the created object) This is defined in the ECMAScript specification (of course, it’s harder to read…) here (Step 9 as of this edit, showing … Read more

Why does JavaScript Promise then handler run after other code?

The relevant specs are here: Promises/A+ point 2.2.4: onFulfilled or onRejected must not be called until the execution context stack contains only platform code. [3.1]. And note 3.1 (emphasis mine): Here “platform code” means engine, environment, and promise implementation code. In practice, this requirement ensures that onFulfilled and onRejected execute asynchronously, after the event loop … Read more

ES6 Promise replacement of async.eachLimit / async.mapLimit

If you don’t care about the results, then it’s quick to whip one up: Promise.eachLimit = async (funcs, limit) => { let rest = funcs.slice(limit); await Promise.all(funcs.slice(0, limit).map(async func => { await func(); while (rest.length) { await rest.shift()(); } })); }; // Demo: var wait = ms => new Promise(resolve => setTimeout(resolve, ms)); async function … Read more