dev-resources.site
for different kinds of informations.
Good bye sleep() and delay() with NodeJS, hello setTimeout()
Published at
11/27/2023
Categories
node
Author
François
Categories
1 categories in total
node
open
Stop to use home-made utilities sleep() and delay(), and use the native nodeJS timers API: setTimeout().
I have seen and added many times in nodeJS codebase the following snippet:
const sleep = (ms) =>
new Promise((resolve) => setTimeout(resolve, ms));
Well, today I learned that nodeJS has a native API for that: setTimeout()
.
// https://nodejs.org/api/timers.html#timerspromisessettimeoutdelay-value-options
import { setTimeout } from 'node:timers/promises';
await setTimeout(2000); // sleep 2s
So time to stop to use home-made utilities sleep()
and delay()
, and use the native nodeJS timers API!
Articles
12 articles in total
Stop using "GitOps" to sell your products
read article
How to increase AWS ELB keep-alive timeout when deployed from Traefik
read article
NodeJS Best Practices: Redacting Secrets from Your Pino Logs
read article
Good bye sleep() and delay() with NodeJS, hello setTimeout()
currently reading
How to use OpenTelemetry to expose custom Prometheus metrics from nodeJS applications
read article
How to stream big JSON files with low-memory footprint in nodeJS
read article
How to debug RDS database from a Kubernetes cluster
read article
Sending slack notifications with github actions
read article
How to add a MySQL Planetscale database as a grafana cloud datasource
read article
Native logging for your NPM package
read article
Traefik High-Availability HTTPS wildcard certificate with Cert-Manager
read article
Post-mortem: 1h30 downtime on a Saturday morning
read article
Featured ones: