Created
May 23, 2023 06:51
-
-
Save fproperzi/619d9a2db999126b9a2ac1c3f8e178ac to your computer and use it in GitHub Desktop.
callBack vs Promise
This file contains 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
// 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