Created
November 11, 2020 23:31
-
-
Save galvao/1b415acfb8fd1c3059c1e7386228ed08 to your computer and use it in GitHub Desktop.
A simple example of Promises in JavaScript
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
console.clear(); | |
function oddOrEven(n) | |
{ | |
return new Promise((resolve, reject) => { | |
if (typeof(n) != 'number') { | |
reject(new TypeError('n must be a number')); | |
} | |
if ((n % 2) == 0) { | |
resolve('even'); | |
} | |
resolve('odd'); | |
}); | |
} | |
oddOrEven(2).then(function (result) { | |
console.log('n is ' + result); | |
}).catch(function (errorObject) { | |
console.error(errorObject.message); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment