Skip to content

Instantly share code, notes, and snippets.

@rodzyn
Last active November 10, 2018 16:48
Show Gist options
  • Select an option

  • Save rodzyn/d3c7c3a0ef9ba55048294a673c99e7cd to your computer and use it in GitHub Desktop.

Select an option

Save rodzyn/d3c7c3a0ef9ba55048294a673c99e7cd to your computer and use it in GitHub Desktop.
Async & Generators
//Async/await implementation via generators
function continueFlow(value) {
generator.next(value);
}
function* createFlow() {
console.log('First');
const apiResponse = yield fetch('https://api.github.com/users/github');
console.log('Third');
console.log(apiResponse.json());
}
const generator = createFlow();
const futureData = generator.next().value;
futureData.then(continueFlow);
console.log('Second');
//Native async/await doing exactly what was done above
async function createFlow() {
console.log('First');
const apiResponse = await fetch('https://api.github.com/users/github');
console.log('Third');
console.log(apiResponse.json());
}
createFlow();
console.log('Second');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment