Skip to content

Instantly share code, notes, and snippets.

@JustAyush
Created May 4, 2020 05:30
Show Gist options
  • Save JustAyush/dc6a261d0dd9c49e4d52c3bbcc12f767 to your computer and use it in GitHub Desktop.
Save JustAyush/dc6a261d0dd9c49e4d52c3bbcc12f767 to your computer and use it in GitHub Desktop.
function makeRequest(location) {
return new Promise((resolve, reject) => {
console.log("making request to " + location);
if(location === "Google") {
resolve('Google says Hi');
} else {
reject('We only talk to Google');
}
})
}
function processRequest(response) {
return new Promise((resolve, reject) => {
console.log("Processing request");
resolve("Extra information " + response);
})
}
// Promise way of doing
makeRequest('Google')
.then((message) => {
console.log(message);
return processRequest(message);
})
.then((extra) => {
console.log(extra);
})
.catch((err) => {
console.log(err);
})
// syntactic sugar with async/await
async function doWork(location) {
try {
const firstResponse = await makeRequest(location);
console.log(firstResponse);
const secondResponse = await processRequest(firstResponse);
console.log(secondResponse);
} catch (e) {
console.log(e);
}
}
doWork("Facebook");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment