Created
September 29, 2021 17:04
-
-
Save eugenioenko/9a3f942f3f636f1b12c3295a7b40fd11 to your computer and use it in GitHub Desktop.
async/await implementation with generator functions and yield
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
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