-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingleton-async.js
More file actions
41 lines (36 loc) · 796 Bytes
/
Copy pathsingleton-async.js
File metadata and controls
41 lines (36 loc) · 796 Bytes
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
// fn return promise
module.exports = (fn) => {
let myFn = null;
let count = 0;
return (...args) => {
if (count > 0) {
count = count + 1;
myFn = myFn.then(() => {
count = count - 1;
return fn(...args);
}).catch((error) => {
console.log(count);
count = 0;
return Promise.reject(error);
});
return myFn;
}
count = count + 1;
myFn = fn(...args);
return myFn.then((result) => {
count = count - 1;
return Promise.resolve(result);
}).catch((error) => {
console.log(count);
count = 0;
return Promise.reject(error);
});
};
};
function* gen(fn) {
let chain = Promise.resolve();
while (true) {
let a = yield chain;
let chain = chain.then(() => a)
}
}