Last active
November 7, 2015 07:56
-
-
Save NotIntMan/97507e8a0bdd8542f473 to your computer and use it in GitHub Desktop.
Just a little module to provide Async functions using ES6 Generators without BabelJS or others transpilers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
function Sync(fn,args) { | |
return new Promise(function(resolve, reject) { | |
let gen, call, callNext, callThrow | |
call = function(isErr, arg) { | |
let result | |
try { | |
if (isErr) | |
result = gen.throw(arg) | |
else | |
result = gen.next(arg) | |
} catch(e) { | |
return reject(e) | |
} | |
if (result.done) | |
return resolve(result.value) | |
else { | |
Promise.resolve(result.value).then(callNext, callThrow) | |
} | |
} | |
callNext = call.bind(null, false) | |
callThrow = call.bind(null, true) | |
gen=fn.apply(this, args) | |
callNext() | |
}); | |
}; | |
Sync.fn=function(fn) { | |
return function() { | |
return Sync.call(this, fn, arguments); | |
}; | |
}; | |
module.exports=Sync; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment