Skip to content

Instantly share code, notes, and snippets.

@bdunn313
Created May 30, 2017 03:20
Show Gist options
  • Save bdunn313/e2569891a53a92869172db6933d626f5 to your computer and use it in GitHub Desktop.
Save bdunn313/e2569891a53a92869172db6933d626f5 to your computer and use it in GitHub Desktop.
// 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