Skip to content

Instantly share code, notes, and snippets.

@kunigami
Last active July 1, 2019 02:53
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 kunigami/f0a678998b2231697012d3f840f14b6e to your computer and use it in GitHub Desktop.
Save kunigami/f0a678998b2231697012d3f840f14b6e to your computer and use it in GitHub Desktop.
function _asyncToGenerator(fn) {
return function () {
var self = this;
var args = arguments;
return new Promise(function (resolve, reject) {
// Instantiates the generator
var gen = fn.apply(self, args);
function _next(value) {
// Next step of the generator
var info = gen.next(value);
var newValue = info.value;
if (info.done) {
resolve(newValue);
} else {
newValue.then(_next);
}
}
// Calls the generator recursively until it's done
_next(undefined);
});
};
}
function f1() {
return Promise.resolve(21);
}
function f2(x) {
return Promise.resolve(x * 2);
}
function g() {
_g = _asyncToGenerator(function* () {
r = yield f1();
s = yield f2(r);
return s;
});
return _g.apply(this, arguments);
}
g().then(r => console.log(r));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment