-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
65 lines (54 loc) · 1.55 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
'use strict'
// Generator returns data from promise with timeout
function * getStreams (nextKey) {
const limit = 10
let done = false
while (!done) {
yield new Promise((resolve, reject) => {
setTimeout(() => {
const result = []
for (let i = nextKey, j = 0; i <= 100, j < limit; i++, j++ ){
result.push(i)
if (i === 100) done = true
}
nextKey = nextKey + limit
resolve({
data: result,
next: nextKey + limit
})
}, 500)
})
}
}
const isPromise = obj => Boolean(obj) && typeof obj.then === 'function'
const next = (iter, callback, prev = undefined) => {
const item = iter.next(prev)
const value = item.value
if (item.done) return callback(prev)
if (isPromise(value)) {
value.then(val => {
setImmediate(() => next(iter, callback, val))
})
} else {
setImmediate(() => next(iter, callback, value))
}
}
const gensync = (fn) =>
(...args) => new Promise(resolve => {
next(fn(...args), val => resolve(val))
})
/* How to use gensync() */
const logs = getStreams(0)
let result = []
const asyncFunc = gensync(function * () {
let result1 = yield logs.next().value // returns promise
result = result.concat(result1.data)
while (result1 && result1.next) {
result1 = yield logs.next().value
if (result1 && result1.data) result = result.concat(result1.data)
}
yield result
})
// Call the async function and pass params.
asyncFunc('param1', 'param2', 'param3', 'params 4')
.then(val => console.log(val)) // 'future value 2