Skip to content

Instantly share code, notes, and snippets.

@danieltnaves
Created July 7, 2019 08:04
Show Gist options
  • Save danieltnaves/8821f1523eb127ac50b9305bd2870b1c to your computer and use it in GitHub Desktop.
Save danieltnaves/8821f1523eb127ac50b9305bd2870b1c to your computer and use it in GitHub Desktop.
Callback x Promise comparison
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