forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
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 <bradley.meck@gmail.com> Co-authored-by: Anatoli Papirovski <apapirovski@mac.com> Refs: nodejs#21152
- Loading branch information
1 parent
86cbad8
commit 36f0340
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); | ||
} |