Promise.resolve().then vs setImmediate vs nextTick

Using Promise.resolve().then has no advantages over nextTick. It runs on the same queue, but have slightly higher priority, that is, promise handler can prevent next tick callback from ever running, the opposite is not possible. This behaviour is an implementation detail and should not be relied on. Promise.resolve().then is obviously slower (a lot, I think), … Read more

Weird behavior with Promise throwing “Unhandled promise rejection” error

In order to be considered handled, rejected promises should be synchronously chained with then(…, …) (2 arguments) or catch(…). promise.then(() => console.log(‘ok’)) is a separate promise that wasn’t chained with catch(…), so rejected promise will result in unhandled rejection. If I’m in any other page (like stackoverflow.com) it throws the exception This isn’t an exception, … Read more

Promise.all is returning an array of undefined and resolves before being done

Promise.all accepts an Array of Promise objects. You’re getting an Array of undefined because you’re not returning any Promise in your map callback: function addText(queries) { return Promise.all(queries.map(function(query) { // Add `return` here or the `map` returns an Array of `undefined`. return models.queries .findById(query.queryId, { raw: true, attributes: [ “query” ] }) .then(function(queryFetched) { query.text … Read more

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

What are the implications of the recursive joining of Promises in terms of Monads?

The first issue can easily be bypassed by always providing a function with the right type a -> Promise a. Or by not using then as the bind operation of the monad, but some type-correct ones. Creed is a functionally minded promise library that provides map and chain methods which implements the Fantasy-land spec for … 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

using await on global scope without async keyword

Update When using Node, the file currently must have an .mjs extension to work. Top level awaits can be used in browser modules. When used the script tag must include the type attribute which must be set to module: <script src=”/script.js” type=”module”></script> const start = Date.now() console.log(‘Pre call.’) await delayedCall() console.log(‘Duration:’, Date.now() – start) function … Read more