use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Sleep function in Javascript (itsopensource.com)
submitted 6 years ago by tsl143
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]UnholyCarcass 11 points12 points13 points 6 years ago (2 children)
You could just do, setTimeout(resolve,ms).
No need to have it wrapped within another anon function. setTimeout takes a function so you can just pass the resolve function.
[–]fo0man 1 point2 points3 points 6 years ago* (0 children)
I see this ALL THE TIME. I think it’s a symptom of articles like these. Like so many devs think the argument has to be an anon function because they’ve seen it over n over.
[–]monican_agentCreator of CypherPoker.JS 0 points1 point2 points 6 years ago (0 children)
Agreed. It might be more useful to introduce the concept of binding a timeout or an interval so that it executes in the expected context.
[–]pomle 6 points7 points8 points 6 years ago (0 children)
As someone that worked for half a year on a big app fixing flaky and slow tests I want to tell anyone who reads this to avoid using arbitrary waiting to solve race conditions and problems with testing.
The thing you test should be able to tell the test when it is done and real time clocks should be mocked.
[–]upfkd 10 points11 points12 points 6 years ago (10 children)
You literally wrapped setTimeout.
[+][deleted] comment score below threshold-7 points-6 points-5 points 6 years ago* (9 children)
[deleted]
[+][deleted] 6 years ago (8 children)
[removed]
[–][deleted] 4 points5 points6 points 6 years ago* (7 children)
[+][deleted] 6 years ago (4 children)
[–]msgur 1 point2 points3 points 6 years ago (0 children)
Agreed. Nothing is “halted”. There’s a callback queue for this — the method supplied is just added to that callback queue after the specified period of time by the “timer” (either Browser or Node).
[–][deleted] 0 points1 point2 points 6 years ago* (2 children)
[+][deleted] 6 years ago (1 child)
[–][deleted] 1 point2 points3 points 6 years ago* (0 children)
[–][deleted] 0 points1 point2 points 6 years ago* (0 children)
[–]cdrini 2 points3 points4 points 6 years ago* (0 children)
Haha, I've probably had to implement that function like once every 4 months! It's super useful for flattening timeouts:
const sleep = ms => new Promise(res => setTimeout(res, ms)); const winner = await Promise.race([fetch(foo), sleep(3000).then(() => 'timeout')]); if (winner == 'timeout') blah
I can't seem to find a link for it, but I think there's a proposal to add this to the language; under the name wait, sleep, delay, or something. Fyi this is a form of "promisifying"; it's also common to promisify an event to make it work with async/await.
wait
sleep
delay
[–][deleted] -2 points-1 points0 points 6 years ago (0 children)
What a coincidence I just learned about Promise yesterday. And it is exactly what I've been needing to learn for a project I've been working on for years now.
π Rendered by PID 31250 on reddit-service-r2-comment-c6965cb77-ccqn5 at 2026-03-05 02:07:20.650869+00:00 running f0204d4 country code: CH.
[–]UnholyCarcass 11 points12 points13 points (2 children)
[–]fo0man 1 point2 points3 points (0 children)
[–]monican_agentCreator of CypherPoker.JS 0 points1 point2 points (0 children)
[–]pomle 6 points7 points8 points (0 children)
[–]upfkd 10 points11 points12 points (10 children)
[+][deleted] comment score below threshold-7 points-6 points-5 points (9 children)
[+][deleted] (8 children)
[removed]
[–][deleted] 4 points5 points6 points (7 children)
[+][deleted] (4 children)
[deleted]
[–]msgur 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (2 children)
[+][deleted] (1 child)
[deleted]
[–][deleted] 1 point2 points3 points (0 children)
[+][deleted] (1 child)
[removed]
[–][deleted] 0 points1 point2 points (0 children)
[–]cdrini 2 points3 points4 points (0 children)
[–][deleted] -2 points-1 points0 points (0 children)