Skip to content

Instantly share code, notes, and snippets.

@anticlergygang
Created December 17, 2019 03:59
Show Gist options
  • Select an option

  • Save anticlergygang/3f2387e8470c47defc08becc37691f8e to your computer and use it in GitHub Desktop.

Select an option

Save anticlergygang/3f2387e8470c47defc08becc37691f8e to your computer and use it in GitHub Desktop.
Here is a simple example of a promise in js
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