Reputation: 2355
What would be the correct way to get the Nodejs timer signature?
Browsers returns integers, but NodeJS returns an internal object which doesn't seem to have a property or method with a parse-able string or number. Is there any way to get this?
Given the following code:
var x = setTimeout(()=>{},1);
console.log(x);
in NodeJS returns:
Timeout {
_called: false,
_idleTimeout: 1,
_idlePrev: [TimersList],
_idleNext: [TimersList],
_idleStart: 2275,
_onTimeout: [Function],
_timerArgs: undefined,
_repeat: null,
_destroyed: false,
domain: [Domain],
[Symbol(unrefed)]: false,
[Symbol(asyncId)]: 73,
[Symbol(triggerId)]: 5 }
while browsers returns (almost random) integers like 3
Upvotes: 0
Views: 689
Reputation: 175
try this solution:
let timerId: ReturnType<typeof setTimeout>
Details: in a browser, the timer identifier is a number. In other environments, this can be something else. For instance, Node.js returns a timer object with additional methods.
ref: the last paragraph before setInterval section
Upvotes: 0
Reputation: 1648
So hi again @blagus,
Your answer actually lives on the docs. Here you can see the referance from nodejs docs which called Symbol.toPrimitive
;
const timer = setTimeout(() => {}, 100);
const timerID = timer[Symbol.toPrimitive]();
clearTimeout(timerID); // you can use this directly on clearTimeout to clear or
// clearTimeout(timer); // just use timer itself =)
console.log(timer[Symbol.toPrimitive]()); // give's you a serializable id which is a number bdw :)
console.log(timer);
Upvotes: 1