Last active
September 11, 2019 19:43
-
-
Save nlitwin/9ed0a6c110071d26a66a07f772b7926d to your computer and use it in GitHub Desktop.
Implementing async / await with generators
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 doWhenDataReceived(value) { | |
returnNextElement.next(value) | |
} | |
function* createFlow() { | |
const data = yield fetch('http://twitter.com/...') | |
console.log(data) | |
} | |
const returnNextElement = createFlow() // Does not start executing function yet | |
const futureData = returnNextElement.next() // starts executing inside createFlow | |
futureData.value.then(doWhenDataReceived) | |
// With async | |
async function createFlow() { | |
const data = await fetch('https://twitter.com/...') | |
console.log(data) | |
} | |
createFlow() |
function doWhenDataReceived(value) {
returnNextElement.next(value);
}
function* createFlow() {
const data =const data = yield fetch('http://twitter.com/...');
console.log(data);
}
const returnNextElement = createFlow();
const futureData = returnNextElement.next();
// the promise is on the `value` key
futureData.value.then(doWhenDataReceived);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uncaught TypeError: futureData.then is not a function at ...