Skip to content

Instantly share code, notes, and snippets.

@NotIntMan
Last active November 7, 2015 07:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NotIntMan/97507e8a0bdd8542f473 to your computer and use it in GitHub Desktop.
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.
'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