Created
April 4, 2020 06:36
-
-
Save manojnaidu619/52f3c8613938ab9896c55a7c307f9044 to your computer and use it in GitHub Desktop.
Promises vs Callbacks in JS
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
// 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