Created
December 17, 2019 03:59
-
-
Save anticlergygang/3f2387e8470c47defc08becc37691f8e to your computer and use it in GitHub Desktop.
Here is a simple example of a promise in js
This file contains hidden or 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
| let addPromise = (a, b) => new Promise((resolve, reject) => { | |
| if (isNaN(a) === false && isNaN(b) === false) { | |
| resolve(a + b) | |
| } else { | |
| reject('a or b, is not a number') | |
| } | |
| }) | |
| addPromise(1, 2).then(resolved => { | |
| console.log(`promise resolved with ${resolved}`) | |
| return addPromise(1, 3) | |
| }).then(resolved => { | |
| console.log(`promise resolved with ${resolved}`) | |
| }).catch(err => { | |
| console.log(`promise rejected with ${err}`) | |
| }) | |
| addPromise(1, 'cookie monster').then(resolved => { | |
| console.log(`promise resolved with ${resolved}`) | |
| }).catch(err => { | |
| console.log(`promise rejected with ${err}`) | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment