1 | type OneByOneNext = (err?: Error) => Promise<any> | any;
|
2 | type OneByOneCallback<T> = (item: T, next: OneByOneNext, index: number) => Promise<any> | any;
|
3 |
|
4 | function createNext<T>(arr: ArrayLike<T>, callback: OneByOneCallback<T>) {
|
5 |
|
6 | let index = 0;
|
7 |
|
8 | const next: OneByOneNext = async (err) => {
|
9 |
|
10 | if (!(index in arr)) {
|
11 | return;
|
12 | }
|
13 |
|
14 | const currentIndex = index++;
|
15 | const item = arr[currentIndex];
|
16 |
|
17 | callback(item, next, currentIndex);
|
18 |
|
19 | };
|
20 |
|
21 | return next;
|
22 |
|
23 | }
|
24 |
|
25 | function oneByOne<T>(arr: ArrayLike<T>, callback: OneByOneCallback<T>) {
|
26 |
|
27 | const next = createNext(arr, callback);
|
28 | next();
|
29 |
|
30 | }
|
31 |
|
32 | export default oneByOne;
|