Skip to content

Instantly share code, notes, and snippets.

@qubyte
Last active October 7, 2015 08:04
Show Gist options
  • Save qubyte/07223ea6ea31ae387e22 to your computer and use it in GitHub Desktop.
Save qubyte/07223ea6ea31ae387e22 to your computer and use it in GitHub Desktop.
Simple asyncRunner. Used as an example for what async-await might desugar to.
function subRunner(gen) {
const state = gen.next();
const thisPromise = Promise.resolve(state.value);
return state.done ? thisPromise : thisPromise.then(subRunner(gen));
}
function asyncWrapper(genFunc) {
return function () {
try {
return subRunner(genFunc.apply(this, arguments));
} catch (e) {
return Promise.reject(e);
}
};
}
module.exports = asyncRunner;
@qubyte
Copy link
Author

qubyte commented Sep 28, 2015

The mapping would look like:

var something = async function (x, y, z) {
  const result = await somethingElse(x);
  // ...
};

to

var something = asyncWrapper(function* (x, y, z) {
  const result = yield somethingElse(x);
  // ...
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment