this post was submitted on 24 Sep 2023
198 points (93.0% liked)

Programmer Humor

32380 readers
1454 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] Tartas1995@discuss.tchncs.de 4 points 1 year ago (1 children)

Yeah and you can void nesting there just as easily and you have the same issues in any other programming language. You just need to create functions. Also JavaScript is not single threaded... you only have access to the dom on one thread, for obvious reasons.

Please explain to me how you do e.g. file downloads without a callback in your favorite language. If you solution involves having the main thread being stuck in a while loop, I am not sure if your complain about nested code can be taken seriously.

[–] yogthos@lemmy.ml -5 points 1 year ago (1 children)

Sure, you end up passing higher order functions around, and my point is that complexity obviously goes up. There's a reason callback hell is a well known thing in Js land. Meanwhile, Js is single threaded from user perspective. The fact that there is a background rendering thread in client side js is completely tangential to the discussion.

Finally, the problem with callbacks is generally seen in server-side Js runtimes. A great example is if you have an HTTP handler that needs to get data from a db. In a language with user accessible threads you just make a db operation synchronously and return the result. In Js, you have to do a callback. The reason you can do the operation synchronously when you have threads is due to the fact that HTTP handler thread can accept a request and then dispatch a new thread to handle it while waiting for other requests.

[–] Tartas1995@discuss.tchncs.de 2 points 1 year ago (1 children)

It is not really tangential to the discussion. You claimed it is because Js single threaded. Also it is not single threaded from the "users" perspective if you mean the developer. There are workers.

If your issue is asynchronous function calls, just call synchronous functions. You might be stuck in a while loop somewhere but if you prefer that, use it. There are sync functions for everything in Js and/or you can easily create them yourself.