Skip to content

Instantly share code, notes, and snippets.

@WoHal
Created May 14, 2020 02:51
Show Gist options
  • Select an option

  • Save WoHal/095fab464d8df55a896cf8c5742a8344 to your computer and use it in GitHub Desktop.

Select an option

Save WoHal/095fab464d8df55a896cf8c5742a8344 to your computer and use it in GitHub Desktop.
function mapLimit(arr, limit, iterator) {
let count = limit;
const next = () => {
if (count > arr.length) {
return;
}
iterator(arr[count], next);
count++;
}
for (let i = 0; i < limit; i++) {
iterator(arr[i], next);
}
}
// example
const data = [1, 2, 3, 4, 5];
const say = function(msg) {
return new Promise(resolve => {
setTimeout(() => {
console.log(msg);
resolve(msg);
}, Math.random() * 1000 * 5);
});
}
mapLimit(data, 2, (item, next) => {
say(item).then(next);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment