timers: captureRejections for timers
The setTimeout()
, setInterval()
, and setImmediate()
functions are
currently not implemented to handle async functions or thenable responses.
setTimeout(async () => { throw new Error('boom'); }, 1000)
Will result in an unhandled rejection that will result in an unhandled rejection warning.
Inspired by @mcollina's recent captureRejections
extension for
EventEmitter
, this implements a similar capability for timers.
const timers = require('timers')
timers.captureRejections = true;
setTimeout(async () => { throw new Error('boom'); }, 1000)
process.on('error', (err) => /* handle here */)
Whether or not this is desirable behavior vs. crashing immediately is a matter that can (and should) be debated.
An alternative approach here would be to make the timer objects into thenables (which I believe has been discussed before)... such that that thenable would reject when an error is thrown, and resolves when cleared.
const i = setInterval(async () => { throw new Error('foo'); }, 1000);
// Only used when function returns a thenable...
i.then(
() => { /** called when clearInterval(i) is called **/ },
(err) => { /** called when throws **/ })
This approach has some API weirdness that would need to be figured out.
/cc @mcollina @BridgeAR
Checklist
-
make -j4 test
(UNIX), orvcbuild test
(Windows) passes -
tests and/or benchmarks are included -
documentation is changed or added -
commit message follows commit guidelines