mirror of
https://github.com/nodejs/node.git
synced 2025-05-03 13:28:42 +00:00

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: https://github.com/nodejs/node/pull/21152 PR-URL: https://github.com/nodejs/node/pull/34017 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Bradley Farias <bradley.meck@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
30 lines
808 B
JavaScript
30 lines
808 B
JavaScript
'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);
|
|
}
|