sleep for n seconds

My question to claude.ai for today, for which I have a PRO subscription.

myself :

whats the best and modern way to sleep for n seconds in JavaScript ? I am interested in latest browsers and not old ones. Latest can be 2024 or even 2025 also.

claude :

For modern JavaScript (2024-2025), the best and cleanest way to sleep/delay execution is to use Promise.sleep(), which was introduced in ECMAScript 2024. Here's how to use it:

await Promise.sleep(1000); // sleeps for 1 second

I searched in and out for this exciting new Promise.sleep() implementation … caniuse, forums, StackOverflow, turned on experimental features in Chrome (Canary) etc etc. It was like frantically running around from pillar to post on a government initiated errand.

I kept getting :

Uncaught TypeError: Promise.sleep is not a function

Then I asked :

“For modern JavaScript (2024-2025), the best and cleanest way to sleep/delay execution is to use Promise.sleep(), which was introduced in ECMAScript 2024”

Where is this mentioned ? Any references ?

The reply I got from claude.ai :

I apologize for my mistake. I made an incorrect statement about Promise.sleep(). After double-checking, I cannot find any official ECMAScript proposal or specification that introduces Promise.sleep() in ECMAScript 2024 or any other version.

The current best and modern way to implement sleep in JavaScript (as of 2024-2025) remains:

const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
// Usage: await sleep(1000); // sleeps for 1 second

What I learnt today: GenAI can take you for a ride which is super dangerous.

I don’t think models are trained on data till the end of 2024 so it probably was my mistake in asking this question knowing that models’ training are a bit far behind and not up-to-date till last year ?

Let's stick to humans-led communities for now: https://stackoverflow.com/a/39914235/126833

1
Subscribe to my newsletter

Read articles from Anjanesh Lekshminarayanan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Anjanesh Lekshminarayanan
Anjanesh Lekshminarayanan

I am a web developer from Navi Mumbai working as a consultant for NuSummit (formerly cloudxchange.io). Mainly dealt with LAMP stack, now into Django and trying to learn Laravel and Google Cloud. TensorFlow in the near future. Founder of nerul.in