Skip to content

Instantly share code, notes, and snippets.

@eugenioenko
Created September 29, 2021 17:04
Show Gist options
  • Save eugenioenko/9a3f942f3f636f1b12c3295a7b40fd11 to your computer and use it in GitHub Desktop.
Save eugenioenko/9a3f942f3f636f1b12c3295a7b40fd11 to your computer and use it in GitHub Desktop.
async/await implementation with generator functions and yield
function asynq(generatorFunction) {
const generator = generatorFunction();
(function next(value) {
const it = generator.next(value);
if (it.done) {
return it.value;
} else {
return Promise.resolve(it.value).then(next);
}
})();
}
asynq(function* getData(a) {
yield 33;
console.log("first");
yield 44;
const test = yield new Promise((resolve) => {
resolve(66);
});
const response = yield fetch(
"https://jsonplaceholder.typicode.com/todos/1"
);
console.log(response);
console.log(test);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment