• 0 Posts
  • 10 Comments
Joined 1 year ago
cake
Cake day: June 30th, 2023

help-circle




  • And what is the alternative? How do you handle any non trivial load in any other language? Without a second thread or while loop. Because apparently you dislike both. You like it sync for your database -> while loop somewhere, but while loops are bad. But asynchronous is bad because it adds complexity to your code when you use functions to reduce the nesting.

    On nodejs, the platform that you talked about earlier, they are literally called worker_threads". So they are different? How? Why can’t you use them?


  • 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.


  • 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.



  • Yes and no. Any programming language encourages nesting as in the end the computer does nest your code. So it is only normal and predictable that languages would reflect that. BUT! Nest logic can often be inverted and by doing so, reduce how much nesting you need to do.

    If (data is not null) {
        If (data has field x) {
             Return data x
        } else return null
    } else return null
    

    Can be

    If (data is null) return null
    If (data hasn't field x) return null
    Return data x