Skip to content

Instantly share code, notes, and snippets.

@novonimo
Created September 13, 2019 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save novonimo/2a4052a51ef114663aa8306664e52f80 to your computer and use it in GitHub Desktop.
Save novonimo/2a4052a51ef114663aa8306664e52f80 to your computer and use it in GitHub Desktop.
learn promise with a single file example
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