-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
41 lines (36 loc) · 785 Bytes
/
index.js
File metadata and controls
41 lines (36 loc) · 785 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
'use strict'
function * sync(count = 2) {
while (1) {
let waiters = []
const then = function(onFulfilled, onReject) {
let resolve, reject
const p = new Promise((res, rej) => {
resolve = res
reject = rej
}).then(onFulfilled, onReject)
if (waiters) {
waiters.push(resolve)
if (waiters.length === count) {
const tmp = waiters
waiters = undefined
for (const waiter of tmp) {
waiter()
}
}
} else {
resolve()
}
return p
}
yield ({
then,
catch(fn) {
return this.then(v => v, fn)
},
finally(fn) {
return this.then(fn)
}
})
}
}
module.exports = sync.sync = sync.default = sync