Last active
October 11, 2022 01:43
-
-
Save danielnass/fc8a46ff95bd9e22d39be8c81a8c1bed to your computer and use it in GitHub Desktop.
JavaScript Async Await from scratch
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){ | |
// 4 - Call .next() passing the value from the Promise | |
fetchFlow.next(value); | |
} | |
function* createFlow(){ | |
// 5 - Assign the Promise fullfiled value to data const | |
const data = yield fetch('https://twitter.com/api/paca/tweets'); | |
// 6 - Console log the data value from the Promise | |
console.log(data); | |
} | |
// 1 - Prepare the flow to execute | |
const fetchFlow = createFlow(); | |
// 2 - Call the .next(), execute the fetch and suspend the execution context on createFlow's yield | |
// leaving the data undefined, saving where the execution context has suspended | |
const executeFetch = fetchFlow.next(); | |
// 3 - When the fetch is fullfiled, execute doWhenDataReceived function passing the value | |
executeFetch.then(doWhenDataReceived); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment