-
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: allow timers to be used as primitives
This allows timers to be matched to numeric Ids and therefore used as keys of an Object, passed and stored without storing the Timer instance. clearTimeout/clearInterval is modified to support numeric/string Ids. Co-authored-by: Bradley Farias <[email protected]> Co-authored-by: Anatoli Papirovski <[email protected]> Refs: #21152 Backport-PR-URL: #34482 PR-URL: #34017 Backport-PR-URL: #34482 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Bradley Farias <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: Yongsheng Zhang <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Signed-off-by: Denys Otrishko <[email protected]>
- Loading branch information
1 parent
14d4bfa
commit 987e0cb
Showing
4 changed files
with
77 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
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,29 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
|
||
[ | ||
setTimeout(common.mustNotCall(), 1), | ||
setInterval(common.mustNotCall(), 1), | ||
].forEach((timeout) => { | ||
assert.strictEqual(Number.isNaN(+timeout), false); | ||
assert.strictEqual(+timeout, timeout[Symbol.toPrimitive]()); | ||
assert.strictEqual(`${timeout}`, timeout[Symbol.toPrimitive]().toString()); | ||
assert.deepStrictEqual(Object.keys({ [timeout]: timeout }), [`${timeout}`]); | ||
clearTimeout(+timeout); | ||
}); | ||
|
||
{ | ||
// Check that clearTimeout works with number id. | ||
const timeout = setTimeout(common.mustNotCall(), 1); | ||
const id = +timeout; | ||
clearTimeout(id); | ||
} | ||
|
||
{ | ||
// Check that clearTimeout works with string id. | ||
const timeout = setTimeout(common.mustNotCall(), 1); | ||
const id = `${timeout}`; | ||
clearTimeout(id); | ||
} |