Skip to content

Instantly share code, notes, and snippets.

@galvao
Created November 11, 2020 23:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save galvao/1b415acfb8fd1c3059c1e7386228ed08 to your computer and use it in GitHub Desktop.
Save galvao/1b415acfb8fd1c3059c1e7386228ed08 to your computer and use it in GitHub Desktop.
A simple example of Promises in JavaScript
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