3 ms·
Defer is more general than using, you can use it for other use cases, for logging logger.log("start") defer logger.log("exit") ... for keeping alive a r
by _old_dude_ 3y ago
Defer is more general than using, you can use it for other use cases, for logging
logger.log("start")
defer logger.log("exit")
...
for keeping alive a reference (because you need to call C code)
global.keepalive = myvar
defer global.keepalive = undefined
...
// call C code here
etc
- Dylan16807 3y agoThose seem like things you'd do multiple places and it would be a good idea to make them into a function. If you have a one-off, defer is nicer looking but using is perfectly capable of doing the job: function defer_it(callback) { return { [Symbol.dispose]: callback }; } // will look better with using void using _ = defer_it(()=>logger.log("exit")); Or use DisposableStack() as another comment in this thread mentions.