Created
May 14, 2020 02:51
-
-
Save WoHal/095fab464d8df55a896cf8c5742a8344 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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