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) | |
} | |
} |