-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
31 lines (30 loc) · 1011 Bytes
/
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
/**
* Run promise in order like promise-queue
* @param Array queue [fn1, fn2, fn3...]
* @return Promise
*/
const order = function(queue){return new Promise(function(resolve, reject) {
const data_arr = [];
const execute = function(queue){
if(queue.length){
const fn = queue.shift();
fn().then((data)=>{
data_arr.push(data);
execute(queue);
}).catch((origin_error)=>{
const err = new Error([
`Error occurred during execurion of Promise.order At index ${data_arr.length}.`,
'Get the succeed data from err.order_data.',
'Get the original error from err.origin_error.'
].join('\n'));
err.order_data = data_arr;
err.origin_error = origin_error;
reject(err);
});
}else{
resolve(data_arr);
}
};
execute(queue);
});};
module.exports = order;