Created
May 30, 2017 03:20
-
-
Save bdunn313/e2569891a53a92869172db6933d626f5 to your computer and use it in GitHub Desktop.
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
// Fake network calls again, same as before. | |
// We don't have to do much besides make it obvious this is async | |
// async/await enhances Promises, it doesn't replace them! | |
function fakeNetworkCall(url) { | |
const promise = new Promise((resolve, reject) => { | |
setTimeout(() => resolve(url), 1500); | |
}); | |
return promise; | |
} | |
// Here's the async function - we can use await inside! | |
async function makeRequest() { | |
try { | |
let firstResponse = await fakeNetworkCall('/api/endpoint/1/'); | |
console.log(firstResponse); | |
let secondResponse = await fakeNetworkCall('/api/endpoint/2/'); | |
console.log(secondResponse); | |
let thirdResponse = await fakeNetworkCall('/api/endpoint/3/'); | |
console.log(thirdResponse); | |
} catch(error) { | |
console.error(error); | |
} | |
} | |
makeRequest(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment