-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
33 lines (27 loc) · 881 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
32
33
const isGeneratorFn = require('is-generator-fn')
const isCallable = require('is-callable')
const curry = require('curry-n')
const effectsResolver = require('./effects/resolver')
const coroutine = (func, ...args) => {
if (isGeneratorFn(func)) {
return evaluateGenerator(func.apply(this, args))
} else if (isCallable(func)) {
return func.apply(this, args)
} else {
return func
}
}
exports = module.exports = curry(2, coroutine)
exports.coroutine = coroutine
const evaluateGenerator = async (gen) => {
let genRightSideDescriptor, genRightSideValue, next
next = gen.next()
genRightSideDescriptor = next.value
while (!next.done) {
genRightSideValue = await effectsResolver(genRightSideDescriptor)
next = gen.next(genRightSideValue)
genRightSideDescriptor = next.value
}
return next.value
}
exports.evaluateGenerator = evaluateGenerator