Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fproperzi/619d9a2db999126b9a2ac1c3f8e178ac to your computer and use it in GitHub Desktop.
Save fproperzi/619d9a2db999126b9a2ac1c3f8e178ac to your computer and use it in GitHub Desktop.
callBack vs Promise
// https://medium.com/recraftrelic/es5-vs-es6-with-example-code-9901fa0136fc
//--- ES5 callBack
function isGreater (a, b, callBack) {
var greater = false
if(a > b) {
greater = true
}
callBack(greater)
}
isGreater(1, 2, function (result) {
if(result) {
console.log('greater');
} else {
console.log('smaller')
}
});
//--- ES6
const isGreater = (a, b) => {
return new Promise ((resolve, reject) => {
if(a > b) {
resolve(true)
} else {
reject(false)
}
})
}
isGreater(1, 2)
.then(result => {
console.log('greater')
})
.catch(result => {
console.log('smaller')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment