-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
50 lines (49 loc) · 1.34 KB
/
index.js
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* Run sync loop with async function
*
* @example
* var syncLoop = require('sync-loop');
* var numberOfLoop = 10;
* syncLoop(numberOfLoop, function (loop) {
* // loop body
* var index = loop.iteration(); // index of loop, value from 0 to (numberOfLoop - 1)
* doAsyncJob(function(){
* // This is callback of your function
* loop.next(); // call `loop.next()` for next iteration
* })
*}, function () {
* console.log("This is finish function")
*});
*/
module.exports = function (iterations, process, exit) {
var index = 0,
done = false,
shouldExit = false;
var loop = {
next: function () {
if (done) {
if (shouldExit && exit) {
return exit(); // Exit if we're done
}
}
// If we're not finished
if (index < iterations) {
index++; // Increment our index
process(loop); // Run our process, pass in the loop
// Otherwise we're done
} else {
done = true; // Make sure we say we're done
if (exit) exit(); // Call the callback on exit
}
},
iteration: function () {
return index - 1; // Return the loop number we're on
},
break: function (end) {
done = true; // End the loop
shouldExit = end; // Passing end as true means we still call the exit callback
}
};
loop.next();
return loop;
};