Skip to content

Instantly share code, notes, and snippets.

@Fishrock123
Last active May 7, 2018 21:18
Show Gist options
  • Save Fishrock123/bac518ad32d022c606744b2c0da82c02 to your computer and use it in GitHub Desktop.
Save Fishrock123/bac518ad32d022c606744b2c0da82c02 to your computer and use it in GitHub Desktop.
unswallowable thenable

an unswallowable thenable

i presently make no claims that this is actually a good idea

WARNING: may not work correctly with async functions

'use strict'
module.exports = function unswallowable (promise) {
return new Unswallowable(promise)
}
class Unswallowable {
constructor (_promise) {
this.promise = _promise
this.hasCatch = false
this.promise.catch(err => {
process.nextTick(_ => {
if (this.hasCatch) return
throw err
})
})
}
then (resolve, reject) {
if (this.hasCatch === false) {
this.hasCatch = typeof reject === 'function'
}
this.promise.then(resolve, reject)
}
catch (fn) {
if (this.hasCatch === false) {
this.hasCatch = typeof fn === 'function'
}
this.promise.catch(fn)
}
finally (fn) {
this.promise.finally(fn)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment