-
Notifications
You must be signed in to change notification settings - Fork 2
/
promise-syncloop.js
37 lines (33 loc) · 992 Bytes
/
promise-syncloop.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
/**
* Provide synchronize looping of task that is based on Promise call.
* It will wait for each asynchronized task to be complete before another one will
* execute, thus gives a feelilng of sycnrhonized for overall tasks to be complete.
*/
/**
* Main execution for synchronized looping of Promise-tasks.
* @param {Number} n Number of loop
* @param {Function} fn Function to be executed. Its signature is fn(index, ...args) in which index is current index in looping, and ...args is arbitrary as per function needs for its parameters
* @param {...Any} args Arguments to be supplied to fn
* @return {Object} Promise object
*/
var main = function(n, fn, ...args) {
var index = 0;
return new Promise((resolve, reject) => {
function next() {
if (index < n) {
fn(index++, ...args)
.then(() => {
next();
})
.catch((err) => {
reject(err);
});
}
else {
resolve();
}
}
next();
});
};
module.exports = main;