Skip to content

Instantly share code, notes, and snippets.

@nexpr
Created March 27, 2016 08:51
Show Gist options
  • Select an option

  • Save nexpr/38682da3b7791f12c5f4 to your computer and use it in GitHub Desktop.

Select an option

Save nexpr/38682da3b7791f12c5f4 to your computer and use it in GitHub Desktop.
async/await by generator
function wait(msec){
return new Promise((resolve, reject) => {
setTimeout(resolve, msec)
})
}
function* async_sample(val){
yield wait(1000)
console.log("after 1second")
console.log(val)
}
async_sample.async("abc")
;(function*(){}).constructor.prototype.async = function(...args){
return new Promise((resolve, reject) => {
var ite = this(...args)
!function recur(pre_value){
var ret = ite.next(pre_value)
ret.done ? resolve(ret.value) : ret.value.then(recur)
}()
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment