Created
May 4, 2020 05:30
-
-
Save JustAyush/dc6a261d0dd9c49e4d52c3bbcc12f767 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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