Omid Sayfun
Omid SayfunComputer Geek
Home
Notebook
Journey

Online

Github
Linkedin
Hevy
Plyatomic
Notebook
The trap of making everything dynamic
March 01, 2024
A try at type-safe groupBy function in TypeScript
April 10, 2025
Email special headers
November 20, 2024
Adding prettier to eslint
April 10, 2025
Storing Vector in Postgres with Drizzle ORM
March 21, 2024
Upgrading my blog to Next 15
April 05, 2025
Canvas macOS issue
February 20, 2024
tsx doesn’t support decorators
March 26, 2025
Validating NestJS env vars with zod
February 06, 2025
Extending Window: types vs interfaces
March 21, 2025
Loading env file into Node process
February 06, 2025
Add update date field to Postgres
February 27, 2024
Using node API for delay
February 06, 2025
React Component Lifecycle
November 28, 2024
How CQRS is different than Event Sourcing
August 18, 2024
RabbitMQ exchange vs queue
August 14, 2024
PgVector similarity search distance functions
August 13, 2024
PgVector indexing options for vector similarity search
July 31, 2024
Using puppeteer executable for GSTS
June 08, 2024
Why EQ is Your Next Career Upgrade
May 13, 2024
Counting GPT tokens
June 30, 2024
Logging route handler responses in Next.js 14
June 19, 2024
Redirect www subdomain with Cloudflare
June 17, 2024
Logging requests in Express app
June 16, 2024
Move Docker volume to bind mount
June 12, 2024
Next.js Hydration Window Issue
May 29, 2024
Using Git rebase without creating chaos in your repo
May 16, 2024
Implementing RPC Calls with RabbitMQ in TypeScript
March 16, 2024
Optimize webpage load with special tags
March 15, 2024
What the hell is Open Graph?
March 13, 2024
My go-to Next.js ESlint config
March 10, 2024
List of useful Chrome args
March 10, 2024
Combining RxJS observables - Part 1
February 20, 2024

Using node API for delay

February 06, 2025 · Updated on February 22, 2025

Ever found yourself needing to pause a process in your Node.js application? Maybe for testing or just to let something else catch up? The go-to method has always been the trusty setTimeout wrapped in a promise. It’s simple and gets the job done:

await new Promise((resolve) => setTimeout(resolve, 1000));

This snippet waits for a second before resolving to undefined. It’s straightforward, but did you know there’s a more elegant way?

Enter Node's Built-in API

Recently, I stumbled upon Node's built-in API that does the same thing, but with a bit more flair. Check this out:

import { setTimeout } from "node:timers/promises";
 
const res = await setTimeout(1000, "result");
 
console.log(res); // Prints 'result'

This method not only waits for a second but also returns a specified value, in this case, 'result'. It’s a neat little upgrade from the classic method.

Optional Parameters for More Control

The new API also allows for some optional parameters through an interface:

Continue Reading

interface Options { ref?: boolean | undefined; signal?: AbortSignal | undefined; }

The ref Option

One of the cool things is the ref option. By setting ref to false, the Node.js event loop can exit if there’s nothing else to do, even if the timeout is still pending. This is super handy for background tasks that shouldn’t block the main execution.

The signal Option

You can also use an AbortSignal to cancel the timeout if needed. This adds a layer of control, allowing you to abort the timer if circumstances change.

tl;dr

Node.js now offers a built-in API for delaying processes with setTimeout from node:timers/promises. It’s a cleaner alternative to the classic promise-wrapped setTimeout, with added options like ref and signal for more control over the event loop and timer cancellation.

  • 02-20-2026

    The trap of making everything dynamic

  • 04-11-2025

    A try at type-safe groupBy function in TypeScript

  • 04-10-2025

    Email special headers

  • 04-10-2025

    Adding prettier to eslint

  • 04-09-2025

    Storing Vector in Postgres with Drizzle ORM