Skip to content

Instantly share code, notes, and snippets.

@manojnaidu619
Created April 4, 2020 06:36
Show Gist options
  • Save manojnaidu619/52f3c8613938ab9896c55a7c307f9044 to your computer and use it in GitHub Desktop.
Save manojnaidu619/52f3c8613938ab9896c55a7c307f9044 to your computer and use it in GitHub Desktop.
Promises vs Callbacks in JS
// Callback
const doWorkCallback = (callback) => {
setTimeout(() => {
//callback(true)
callback(false)
},2000)
}
doWorkCallback((result) => {
if (!result) {
return console.log(result)
}
console.log(result)
})
// Promises
const doWorkPromise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve(true)
//reject(false)
},2000)
})
doWorkPromise
.then((res) => console.log(res))
.catch((err) => console.log(err))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment