Home > other >  Can we use the _destroyed property of Node.js timers?
Can we use the _destroyed property of Node.js timers?

Time:11-23

The timers created by setTimeout in Node.js have a _destroyed property that would be nice to use for diagnostics (read-only).

E. g. in the REPL it works like this:

> let timer = setTimeout(() => console.log('elapsed'), 20000); console.log(timer._destroyed)
false
undefined
> elapsed
> timer._destroyed
true

But it's a property that begins with an underscore.

Should _destroyed not be used from outside the object at all (because the API is unstable etc)? Or is it fine to use it as long as we avoid setting it from the outside?

CodePudding user response:

I'd advice against using it.

As you say, properties beginning by an underscore in JS are the historical way of marking properties as private and subject to removal or change of behavior without it being documented, so there's not guarantee it'll work as intended.

Also, ._destroyed is not a documented property of the Timeout class, so it also supports the advice of not using it.

  • Related