Skip to content

Instantly share code, notes, and snippets.

@bbenjamin
Created February 13, 2022 23:10
Show Gist options
  • Save bbenjamin/35f31b9d66517866cc2f65528bba77a1 to your computer and use it in GitHub Desktop.
Save bbenjamin/35f31b9d66517866cc2f65528bba77a1 to your computer and use it in GitHub Desktop.
Async await vs promise chain
const callDataMuse = function() {
const parseJson = function(response) {
return response.json();
}
const logParsedJson = function(parsedJson) {
console.log('parsed json!', parsedJson);
}
fetch('https://api.datamuse.com/words?sl=butter')
.then(parseJson)
.then(logParsedJson)
}
callDataMuse();
// THIS ALSO WORKS
const callDataMuse2 = async function() {
const parseJson = async function(response) {
return response.json();
}
const logParsedJson = function(parsedJson) {
console.log('parsed json!', parsedJson);
}
const response = await fetch('https://api.datamuse.com/words?sl=butter');
const parsedJson = await parseJson(response);
logParsedJson(parsedJson);
}
callDataMuse2();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment