Created
September 13, 2019 11:59
-
-
Save novonimo/2a4052a51ef114663aa8306664e52f80 to your computer and use it in GitHub Desktop.
learn promise with a single file example
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
function makeReques(location) { | |
return new Promise((resolve, reject) => { | |
console.log(`Making requrest to ${location}`) | |
if (location === "Google") { | |
resolve("Google says hi") | |
} else { | |
reject("We can only talk to Google") | |
} | |
}) | |
} | |
function processRequest(response) { | |
return new Promise((resolve, reject) => { | |
console.log("Processing response") | |
resolve(`Extra Information + ${response}`) | |
}) | |
} | |
/* use then, catch block */ | |
// makeReques("Google").then(response => { | |
// console.log("Response recevied ") | |
// return processRequest(response) | |
// }).then(processResponse => { | |
// console.log(processResponse) | |
// }).catch(err => { | |
// console.log(err) | |
// }) | |
/* use async/await block */ | |
async function doWork () { | |
try { | |
const response = await makeReques("Google") | |
console.log("response recevied") | |
const processResponse = await processRequest(response) | |
console.log(processResponse) | |
} catch (err) { | |
console.log(err) | |
} | |
} | |
doWork() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment