This repository was archived by the owner on Sep 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
36 lines (34 loc) · 1.3 KB
/
Copy pathutils.js
File metadata and controls
36 lines (34 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// https://mostafa-samir.github.io/async-iterative-patterns-pt1/
function waterfallOverArray (list, iterator, callback) {
var nextItemIndex = 0; //keep track of the index of the next item to be processed
function report () {
nextItemIndex++;
// if nextItemIndex equals the number of items in list, then we're done
if(nextItemIndex === list.length)
callback();
else
// otherwise, call the iterator on the next item
iterator(list[nextItemIndex], report);
}
// instead of starting all the iterations, we only start the 1st one
iterator(list[0], report);
}
// Modified version of the original WaterfallOver object, to iterate through objects instead of arrays.
function waterfallOverObject (obj, iterator, callback) {
var nextItemIndex = 0; //keep track of the index of the next item to be processed
function report () {
nextItemIndex++;
// if nextItemIndex equals the number of items in object, then we're done
if(nextItemIndex === Object.keys(obj).length)
callback();
else
// otherwise, call the iterator on the next item
iterator(Object.keys(obj)[nextItemIndex], report);
}
// instead of starting all the iterations, we only start the 1st one
iterator(Object.keys(obj)[0], report);
}
module.exports = {
waterfallOverArray: waterfallOverArray,
waterfallOverObject: waterfallOverObject
}