Promise packages, patterns, chat, and tutorials
5,168
stars
37
commits
Apr 25, 2024
updated
I intend to use this space to document my promise modules, useful promise patterns, and how to solve common problems. For now though, you can see all my promise modules below.
Not accepting additions, but happy to take requests.
Promise.all() but for Map and Object.then() or .catch() is calledPromise.try() ponyfill - Starts a promise chainPromise.race()setImmediate().then/.catch-based packagesYou should generally avoid using .then except in edge cases.
This is a good use-case for p-map. You might ask why you can't just specify an array of promises. Promises represent values of a computation and not the computation itself - they are eager. So by the time p-map starts reading the array, all the actions creating those promises have already started running. p-map works by executing a promise-returning function in a mapper function. This way the promises are created lazily and can be concurrency limited. Check out p-all instead if you're using different functions to get each promise.
import pMap from 'p-map';
const urls = [
'https://sindresorhus.com',
'https://avajs.dev',
'https://github.com',
…
];
console.log(urls.length);
//=> 100
const mapper = url => fetchStats(url); //=> Promise
const result = await pMap(urls, mapper, {concurrency: 5});
console.log(result);
//=> [{url: 'https://sindresorhus.com', stats: {…}}, …]
Promise packages, patterns, chat, and tutorials
5,168
stars
37
commits
Apr 25, 2024
updated
I intend to use this space to document my promise modules, useful promise patterns, and how to solve common problems. For now though, you can see all my promise modules below.
Not accepting additions, but happy to take requests.
Promise.all() but for Map and Object.then() or .catch() is calledPromise.try() ponyfill - Starts a promise chainPromise.race()setImmediate().then/.catch-based packagesYou should generally avoid using .then except in edge cases.
This is a good use-case for p-map. You might ask why you can't just specify an array of promises. Promises represent values of a computation and not the computation itself - they are eager. So by the time p-map starts reading the array, all the actions creating those promises have already started running. p-map works by executing a promise-returning function in a mapper function. This way the promises are created lazily and can be concurrency limited. Check out p-all instead if you're using different functions to get each promise.
import pMap from 'p-map';
const urls = [
'https://sindresorhus.com',
'https://avajs.dev',
'https://github.com',
…
];
console.log(urls.length);
//=> 100
const mapper = url => fetchStats(url); //=> Promise
const result = await pMap(urls, mapper, {concurrency: 5});
console.log(result);
//=> [{url: 'https://sindresorhus.com', stats: {…}}, …]