-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
53 lines (53 loc) · 1.93 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
/**
* asink
* =====
*
* asink is the same thing as, or a rename of, spawn. spawn in turn is a tool
* for repeatedly calling the .thens of promises yielded by a generator.
* Basically, this makes it possible to write asynchronous, promisified code
* with normal try/catches that look just like synchronous code. It creates
* shorter and easier to understand code. Hypothetically, there will be a
* feature in the next version of javascript, ES7, called "async functions",
* which do exactly what asink does. When/if that happens and we can access it
* in node, we can simply remove all calls to asink and our code should behave
* in the same way.
*
* See:
* http://tc39.github.io/ecmascript-asyncawait/
* https://github.com/tc39/ecmascript-asyncawait
* https://gist.github.com/jakearchibald/31b89cba627924972ad6
* http://www.html5rocks.com/en/tutorials/es6/promises/
* https://blogs.windows.com/msedgedev/2015/09/30/asynchronous-code-gets-easier-with-es2016-async-function-support-in-chakra-and-microsoft-edge/
*/
'use strict'
function spawn (genF, self) {
return new Promise(function (resolve, reject) {
var gen = genF.call(self)
function step (nextF) {
var next
try {
next = nextF()
} catch (e) {
// finished with failure, reject the promise
if (typeof process !== 'undefined' && process.env && process.env.ASINK_LOG_ERRORS === 'true') {
console.error('asink error:', e)
}
reject(e)
return
}
if (next.done) {
// finished with success, resolve the promise
resolve(next.value)
return
}
// not finished, chain off the yielded promise and `step` again
Promise.resolve(next.value).then(function (v) {
step(function () { return gen.next(v) })
}, function (e) {
step(function () { return gen.throw(e) })
})
}
step(function () { return gen.next(undefined) })
})
}
module.exports = spawn