run promises in chunks
npm install chunk-promisechunk-promise is a tiny library that can be used to run a list of native promises in chunks/patches by creating a promise chain with some optional customization that gives you full control over these promises.
It supports running both Promise.all and Promise.allSettled flavors in chunks.
It can be used to run Promise.allSettled in browsers that do not support it.
It can be combined with async/await.
_What is Promise.allSettled? see here_
Yes, there are lots of libraries that give you some control over promises.
bluebird: This library does not use native promises, it is using its own version of promises.
p-limit: It does use native promises but it neither support Promise.allSettled nor callbacks between chunks.
Well, it depends in your situation. If you want to use either native promises or async/await they are definitely doing great however both Promise.all and Promise.allSettled would run all functions in parallel with no control over how many promises to run in patches. they do not support running promises in chunks. You simply have a list of promises that run all in parallel.
- If you want to run promises in chunks with both Promise.all and Promise.allSettled flavors where every single chunk runs (n) number of promises in parallel.
- If you want to slow down the execution by introducing sleep/timeout function between chunks.
- If you want to call a custom function after every single chunk.
- If you want to force stop the promises execution for some reason in the middle.
- If you want to use Promise.allSettled in browsers that do not support it, or in Node (only supported natively in Node starting from version 12)
All of these functionality are packed inside one small yet powerful library.
```
npm install chunk-promise
| Option | Required? | Description | Default value |
| ----------------- | --------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------- |
| concurrent | No | Number of concurrent promises to run in a single chunk | Infinity |
| sleepMs | No | Sleep function in milliseconds between chunks | undefined |
| callback | No | Callback async function to be called after every single chunk.
It exposes 3 params:
- chunkResults: the current chunk value. chunkIndex
- : the current chunk index.allResults
- : the results of the promises so far. | undefined |Promise.all
| promiseFlavor | No | Choose between and Promise.allSettled | Promise.all |
| logMe | No | Log what will be running | false |
`javascript
const { chunkPromise, PromiseFlavor } = require('chunk-promise');
const promiseArr = [
() => Promise.resolve(1),
() => Promise.reject(2),
() => Promise.resolve(3),
() => Promise.reject(4),
() => Promise.resolve(5)
];
chunkPromise(promiseArr, {
concurrent: 2,
promiseFlavor: PromiseFlavor.PromiseAll
})
.then(res => {})
.catch(err => {});
`
`javascript
const { chunkPromise, PromiseFlavor } = require('chunk-promise');
const promiseArr = [
() => Promise.resolve(1),
() => Promise.reject(2),
() => Promise.resolve(3),
() => Promise.reject(4),
() => Promise.resolve(5)
];
chunkPromise(promiseArr, {
concurrent: 2,
promiseFlavor: PromiseFlavor.PromiseAllSettled
}).then(res => {});
`
`javascript
const { chunkPromise, PromiseFlavor } = require('chunk-promise');
const promiseArr = [
() => Promise.resolve(1),
() => Promise.reject(2),
() => Promise.resolve(3),
() => Promise.reject(4),
() => Promise.resolve(5)
];
chunkPromise(promiseArr, {
concurrent: 2,
promiseFlavor: PromiseFlavor.PromiseAll,
sleepMs: 2000
})
.then(res => {})
.catch(err => {});
`
`javascript
const { chunkPromise, PromiseFlavor } = require('chunk-promise');
const promiseArr = [
() => Promise.reject(1),
() => Promise.reject(2),
() => Promise.resolve(3),
() => Promise.reject(4),
() => Promise.resolve(5)
];
chunkPromise(promiseArr, {
concurrent: 2,
promiseFlavor: PromiseFlavor.PromiseAllSettled,
callback: async (chunkResults, index, allResults) => {
if (chunkResults.some(p => p.status === 'fulfilled')) {
console.log(chunk (${index}): has success results);chunk (${index}): has no success results
} else {
console.log();`
}
}
}).then(res => {});
`javascript
const {
chunkPromise,
PromiseFlavor,
ChunkPromiseCallbackForceStopError
} = require('chunk-promise');
const promiseArr = [
() => Promise.resolve(1),
() => Promise.resolve(2),
() => Promise.resolve(3),
() => Promise.resolve(4),
() => Promise.resolve(5)
];
chunkPromise(promiseArr, {
concurrent: 2,
promiseFlavor: PromiseFlavor.PromiseAll,
callback: async (chunkResults, index, allResults) => {
console.log(chunk (${index}): has success results);Callback force stop at chunk index ${index}
if (index === 1) {
throw new ChunkPromiseCallbackForceStopError(
``
);
}
}
})
.then(res => {})
.catch(err => {
if (err instanceof ChunkPromiseCallbackForceStopError) {
console.log('Force stop');
} else {
console.log('failed');
}
});
Please do not hesitate to report any issue/improvement if you want to add more options/customizations to this library.