i mean it blocks the main thread and sleeps, whats wrong with it? the name "Sync" in context of node.js means something which blocks the main thread and does an action synchronously. you could argue that Date.now() should have been used instead of creating so many Date objects and calling the getTime() method on each, but other than that, this is fine.
Edit: on second thought, i can't think of a reason a developer would want to do a sleepSync(). they probably want to only block in their own function/stack context, while the main thread/event loops is free to continue as normal. so await sleep() would likely be more appropriate. I'm 50/50 on whether this is bad code, i would have to know what the use case is and why they are blocking the whole program with a synchronous sleep.
an event loop is a system which allows one to author non blocking programs, by offloading heavy work to background threads, then returning the result to the main thread via "events". A good example is the following library, which node.js uses under the hood for its event loop, as do other programs, such as nginx webserver.
It has its place. Without context, it's hard to tell if this is a good use or not, but given that it's JS as opposed to c/C++, it's very unlikely that this is part of a realtime system where you can't risk letting a thread go cold.
node.js is multithreaded under the hood and always has been. It's true that we used to not have multi thread capability as developers, but we actually have for several years now, but most developers have probably never used it. Even prior to Worker Threads/Shared Buffers, we had 3rd party modules such as node-fiber and various shared memory data structure libraries which could be used to build multi threaded javascript programs. https://github.com/wilk/microjob <- dead simple batteries included multi threading in node.js
Yes, but busy waiting won't effect them, since you have no control on which thread it gets executed. The chance that this is a performance thing is near 0.
Official (new) reddit web / app do this. They also strip the underscores before rendering so that people using them are completely unaware of the broken links.
setTimeout itself uses callbacks, so its technically async, but I'd argue there is rarely a need to "sleepSync" in the first place. Plus the method provided by OP is inefficient af with all these date instances being created.
it is customary in node.js to notify other developers your function is synchronous, because node.js is asynchronous, and blocking the main thread is generally bad for most use cases. you would expect a non blocking sleep to be called sleep() and a blocking one to be called sleepSync(). a non blocking sleep would likely be implemented with promises, so the developer can just do await sleep(200);, only blocking the function they are in, and not the entire program. that could be what the op was getting at, and i didn't really think about it when i replied, but now that i think of it, i can't see any reason a person would desire to do a sleepSync()
Sure, but then it’s not really synchronous, you just write the code in a way that looks synchronous.
But maybe they’re in a situation where they can’t use async/await, like at the top level on a runtime that doesn’t support top-level await (although I guess they could just stick it in an async IIFE in that case) or maybe they’re writing JS for something like the Espruino.
They probably could’ve used async/await given that this looks like either Typescript or Flow (which means it’d probably be transpiled anyways), but I’m just nitpicking that it wouldn’t really be synchronous in that case.
I'm not exactly sure what you mean by "true async". You can't have multiprocessing without threads, but you can absolutely have async code, just like you can't have multiprocessing without multiple cores but can absolutely have multithreaded code. If you compare async with multithreading in a single core scenario, async just ends up looking like userspace threads.
I’m probably wrong, but in my mind the cpu thread is waiting for the async call to return before it does anything else and another thread is doing the main logic loop. And in systems with only one thread available there is task switching going on, which is not really multi threading afaik.
The people in here arguing this has its legitimate uses are even more funny than the bad code itself. People who write javascript really don't understand how computers work, like at all do they? lmao
i'm just not certain there *isn't* a valid use case for synchronous sleep in javascript. there may very well be a valid use case. how can you be so sure?
I think I once wrote a sync sleep like this to debug a function that was sync, so I didn't have to rewrite it (and all its callers) to async. But when I was done I removed it again :D
I can't imagine why you'd busy wait in JavaScript, but busy waits, especially in the context of spinlocks, are absolutely everywhere in kernelspace. If a lock is unlikely to be held at any given time because it's only held for short periods, spinlocks are very efficient because they avoid context switches.
If someone actually needs a syncronous sleep in JavaScript, this is a great way to do it. Doesn't look like bad code without the surrounding context to judge on.
93
u/187mphlazers Mar 24 '22 edited Mar 24 '22
i mean it blocks the main thread and sleeps, whats wrong with it? the name "Sync" in context of node.js means something which blocks the main thread and does an action synchronously. you could argue that
Date.now()should have been used instead of creating so manyDateobjects and calling thegetTime()method on each, but other than that, this is fine.Edit: on second thought, i can't think of a reason a developer would want to do a
sleepSync(). they probably want to only block in their own function/stack context, while the main thread/event loops is free to continue as normal. soawait sleep()would likely be more appropriate. I'm 50/50 on whether this is bad code, i would have to know what the use case is and why they are blocking the whole program with a synchronous sleep.29
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Mar 24 '22
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
16
u/PaulMorel Mar 24 '22
jfc. This isn't in a production system, right? Like, this is a student? Or it's made up?
5
u/O_X_E_Y Mar 24 '22
truly beautiful
setTimeout? Never heard of it
5
u/_SAMUEL_GAMING_ Mar 24 '22
this could work if you dont plan to use multiple threads and other async stuff
17
u/[deleted] Mar 24 '22
The people in here arguing this has its legitimate uses are even more funny than the bad code itself. People who write javascript really don't understand how computers work, like at all do they? lmao
4
u/kbruen Mar 25 '22
If someone actually needs a syncronous sleep in JavaScript, this is a great way to do it. Doesn't look like bad code without the surrounding context to judge on.
1
u/lacifuri Mar 25 '22
I saw the answer by Reema: https://stackoverflow.com/questions/1141302/is-there-a-sleep-function-in-javascript Same spirit
1
u/Aphix Mar 25 '22
What are Atomics? Hint: They are the correct answer for a sync solution to this without a busy wait.
1
u/[deleted] Mar 25 '22
setTimeout() ? What's that?
1
u/Voltra_Neo Mar 24 '22
This should really be:
0
u/[deleted] Mar 24 '22
[deleted] — view deleted comment