# Unblocking Node With Unref()

DevFeed: [Unblocking Node With Unref()](<https://devfeed.tech/articles/unblocking-node-with-unref-19100.md>)

Original publisher: [Read original article](<https://httptoolkit.com/blog/unblocking-node-with-unref/>)

Author: HTTP Toolkit; Tim Perry

Published: 2019-09-11T13:30:00Z

Content type: tutorial

Language: en

Sources: [HTTP Toolkit](<https://devfeed.tech/sources/http-toolkit.md>)

Topics: [Node.js](<https://devfeed.tech/topics/node-js.md>), [Processes](<https://devfeed.tech/topics/processes.md>), [interception](<https://devfeed.tech/topics/interception.md>), [HTTP](<https://devfeed.tech/topics/http.md>)

Tags: [code](<https://devfeed.tech/tags/code.md>), [errors](<https://devfeed.tech/tags/errors.md>), [http](<https://devfeed.tech/tags/http.md>), [interception](<https://devfeed.tech/tags/interception.md>), [node-js](<https://devfeed.tech/tags/node-js.md>), [process](<https://devfeed.tech/tags/process.md>), [timeout](<https://devfeed.tech/tags/timeout.md>)

## AI overview

This tutorial explains how Node.js timers keep the event-loop process alive and how calling unref() allows timers to continue running without preventing the process from exiting when no other work remains. It also discusses why explicit cleanup can still be useful and notes a potential performance cost.

## Source excerpt

Node.js runs on an event loop. It holds a queue of tasks to run, and runs them, one by one. New tasks appear on the queue while it runs, added by your code (setTimeout) or outside events (a network connection), and the process simply continues until the queue is empty. That's all great, until it isn't. Occasionally you want to break out of that model. What happens if you want to run a schedule task on a fixed interval indefinitely? Typically, life gets difficult: you need to include & manage an explicit shutdown process for that interval, and if you ever forget to shut it down then the process will keep running forever, with no explanation. Ouch. I hit ran into this whilst working on Mockttp (the HTTP interception & testing library behind HTTP Toolkit). Mockttp needs to keep track of your current local IP addresses, to help detect and warn about request loops. That data can change occasionally, so it needs to poll it on an interval, but it's very annoying to have to remember to carefully shut that process down in addition to everything else. Fortunately, it turns out you can fix this easily! Enter unref: Timeout.Unref() Timer functions like setInterval and setTimeout in Node.js return a Timeout object, representing the ongoing timer. These can be passed to clearInterval or clearTimeout to shutdown the timer entirely, but they also have a little-used unref() method. This does something magical: it keeps running your code, but stops it from keeping the process alive. Like so: // Update my data every 10 seconds const interval = setInterval(() => updateMyData(), 10000); // But don't let that keep the process alive! interval.unref(); // Log a message if the app is still running 10 seconds from now const timeout = setTimeout(() => console.log('Still going'), 10000); // But still shutdown cleanly if it wants to stop before then: timeout.unref(); This functions like a flag you can set on your timers, marking them as tasks that node doesn't need to wait for. They'll run as n