Skip to content

Instantly share code, notes, and snippets.

@danielnass
Last active October 11, 2022 01:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielnass/fc8a46ff95bd9e22d39be8c81a8c1bed to your computer and use it in GitHub Desktop.
Save danielnass/fc8a46ff95bd9e22d39be8c81a8c1bed to your computer and use it in GitHub Desktop.
JavaScript Async Await from scratch
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