Created
July 7, 2019 08:04
-
-
Save danieltnaves/8821f1523eb127ac50b9305bd2870b1c to your computer and use it in GitHub Desktop.
Callback x Promise comparison
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
const sampleCallBack = (callback) => { | |
setTimeout(() => { | |
callback('This is an error', undefined) | |
}, 2000) | |
} | |
sampleCallBack((error, result) => { | |
if (error) { | |
return console.log(error) | |
} | |
console.log(result) | |
}) | |
const samplePromise = new Promise((resolve, reject) => { | |
setTimeout(() => { | |
resolve('Hi there') | |
//reject('Hi there') | |
}, 2000); | |
}) | |
samplePromise.then((result) => { | |
console.log('success', result) | |
}).catch((error) => { | |
console.log('error', error) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment