-
Notifications
You must be signed in to change notification settings - Fork 0
/
asyncawait.js
36 lines (35 loc) · 968 Bytes
/
asyncawait.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
"use strict"
function async(generatorsource) {
return(new Proxy(generatorsource, {
apply(target, that, inputs) {
return(new Promise(function(resolve, reject) {
let generator = Reflect.apply(target, that, inputs)
function proceed(type = "next" || "throw", input) {
try {
let result
if(type == "throw") {
result = generator.throw(input)
} else {
result = generator.next(input)
}
if(result.done) {
resolve(result.value)
} else {
Promise.resolve(result.value).then(function(result) {
proceed("next", result)
}).catch(function(error) {
proceed("throw", error)
}) }
} catch(error) {
reject(error)
} }
proceed("next")
})) },
get(target, key) {
if(key == "toString") {
return(function() {
return(generatorsource.toString().replace("function*", "async function"))
})
} else {
return(Reflect.get(target, key))
} } })) }