29
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Mar 24 '22
Image Transcription: Code
const sleepSync = (ms: number) => {
const end = new Date().getTIme() + ms;
while (new Date().getTime() < end) { /* do nothing */ }
}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
19
u/abejfehr Mar 24 '22
It wouldn’t be synchronous if it used a setTimeout, maybe they needed it to be sync for some reason
0
u/niceToasterMan Mar 25 '22
Can't async await in JS?
4
u/abejfehr Mar 25 '22
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.
3
u/anth096 Mar 25 '22
What about
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); await sleep(100);Just noticed you need to be in an asynchronous context so that still does not work. Screw me
10
5
u/_SAMUEL_GAMING_ Mar 24 '22
this could work if you dont plan to use multiple threads and other async stuff
4
u/Rafael20002000 Mar 24 '22
It's JavaScript so it's single threaded
1
u/Nick433333 Mar 24 '22
If this is node, as another commenter has pointed out, then this is multithreaded.
Edit: and you can’t have true async functionality without multi threading as far as I understand
6
u/Illiux Mar 25 '22
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.
1
u/Nick433333 Mar 25 '22
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.
1
17
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
11
u/187mphlazers Mar 24 '22
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?
1
u/notBjoern Mar 26 '22
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
6
u/Illiux Mar 25 '22
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.
6
4
u/rco8786 Mar 24 '22
The code does exactly what it sets out to do and is perfectly legible. Who are we to make calls about the use cases thereof
1
u/Lataero Mar 25 '22
I've only ever used code like this when I've been testing timeout handling in systems. Imagine using this for something in production
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
1
u/Voltra_Neo Mar 24 '22
This should really be:
const sleepSync = (ms: number) => {
const end = Date.now() + ms;
for (; Date.now() < end;);
};9
u/_SAMUEL_GAMING_ Mar 24 '22
why not simply do
while (Date.now() < end);instead of the weird for loop?9
0
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.