-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
timers: fail early when callback is not a function
`setTimeout()`, `setInterval()` and `setIntermediate` currently throw errors when receiving non-function objects as their first argument, but only do so when trying to execute the callback, i.e. after the waited time has passed. This may complicate debugging when a lot of calls to `setTimeout()`/etc. are involved, so failing as early as possible seems like a good idea. `setTimeout()` historically ignored an falsy first argument, while the other functions do not and throw instead. This patch changes this behaviour to make all three match and adds remarks in the corresponding documentation. PR-URL: #4362 Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Brian White <[email protected]> Reviewed-By: Trevor Norris <[email protected]> Reviewed-By: Rod Vagg <[email protected]>
- Loading branch information
Showing
3 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
|
||
function doSetTimeout(callback, after) { | ||
return function() { | ||
setTimeout(callback, after); | ||
}; | ||
} | ||
|
||
assert.throws(doSetTimeout('foo'), | ||
/"callback" argument must be a function/); | ||
assert.throws(doSetTimeout({foo: 'bar'}), | ||
/"callback" argument must be a function/); | ||
assert.throws(doSetTimeout(), | ||
/"callback" argument must be a function/); | ||
assert.throws(doSetTimeout(undefined, 0), | ||
/"callback" argument must be a function/); | ||
assert.throws(doSetTimeout(null, 0), | ||
/"callback" argument must be a function/); | ||
assert.throws(doSetTimeout(false, 0), | ||
/"callback" argument must be a function/); | ||
|
||
|
||
function doSetInterval(callback, after) { | ||
return function() { | ||
setInterval(callback, after); | ||
}; | ||
} | ||
|
||
assert.throws(doSetInterval('foo'), | ||
/"callback" argument must be a function/); | ||
assert.throws(doSetInterval({foo: 'bar'}), | ||
/"callback" argument must be a function/); | ||
assert.throws(doSetInterval(), | ||
/"callback" argument must be a function/); | ||
assert.throws(doSetInterval(undefined, 0), | ||
/"callback" argument must be a function/); | ||
assert.throws(doSetInterval(null, 0), | ||
/"callback" argument must be a function/); | ||
assert.throws(doSetInterval(false, 0), | ||
/"callback" argument must be a function/); | ||
|
||
|
||
function doSetImmediate(callback, after) { | ||
return function() { | ||
setImmediate(callback, after); | ||
}; | ||
} | ||
|
||
assert.throws(doSetImmediate('foo'), | ||
/"callback" argument must be a function/); | ||
assert.throws(doSetImmediate({foo: 'bar'}), | ||
/"callback" argument must be a function/); | ||
assert.throws(doSetImmediate(), | ||
/"callback" argument must be a function/); | ||
assert.throws(doSetImmediate(undefined, 0), | ||
/"callback" argument must be a function/); | ||
assert.throws(doSetImmediate(null, 0), | ||
/"callback" argument must be a function/); | ||
assert.throws(doSetImmediate(false, 0), | ||
/"callback" argument must be a function/); |