Last active
November 10, 2018 16:48
-
-
Save rodzyn/d3c7c3a0ef9ba55048294a673c99e7cd to your computer and use it in GitHub Desktop.
Async & Generators
This file contains hidden or 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
| //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