-
-
Save Anmo/cdf674e09644f24ea58bef327e8016b3 to your computer and use it in GitHub Desktop.
Check the original: https://gist.github.com/JosePedroDias/76bbb68ade995751ffc6a2fbc2597ae9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const split = (arr = [], n, ...arrs) => arr.length ? split(arr.slice(n), n, ...arrs, arr.slice(0, n)) : arrs | |
const splitIn = (arr = [], n) => split(arr, Math.ceil(arr.length / n)) | |
const flat = (arr) => arr.reduce((a, b) => a.concat(b), []) | |
const mapParallel = (prom) => (arr) => Promise.all(arr.map(prom)) | |
const mapSeries = (arr, prom, prevProm = Promise.resolve()) => | |
Promise.all(arr.map(val => (prevProm = prevProm.then(() => prom(val))))) | |
const allLimit = (arr, limit) => ({ then: (prom) => mapSeries(split(arr, limit), mapParallel(prom)).then(flat) }) | |
const mapLimit = (arr, prom, limit) => mapSeries(split(arr, limit), mapParallel(prom)).then(flat) | |
const _mapLimit = (arr, prom, limit) => allLimit(arr, limit).then(prom) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const sleep = (ms = 100) => | |
(n) => | |
new Promise(resolve => { | |
console.log('FIRED ' + n); | |
setTimeout(() => { | |
console.log(n) | |
resolve(n) | |
}, ms) | |
}) | |
var arr = [1, 2, 3, 4, 5, 6, 7] | |
mapLimit(arr, sleep(500), 3).then(r => console.log(r)) | |
allLimit(arr, 3).then(sleep(500)).then(r => console.log(r)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment