24
linuxxx
27d

JavaScript Motherfucking Asynchronous Bullshit.

I get it, for quite some stuff, async is very, very useful. But why on fucking earth do so goddamn many functions NEED this (and those callback functions) and can't do without?!

If there would be good and nicely understandable await documentation that actually fucking works, I'd be so happy.

I've currently got .then after .then after motherfucking then and its irritating me to no end as it, in this context, shouldn't even be necessary. This thing I'm writing doesn't give a fuck if something takes a few milliseconds before the rest of the program can continue!!

Fuck asynchronous programming in JavaScript for goddamn everything.

(I do love JavaScript!)

Comments
  • 3
    I agree, I hate that I can't choose whether I want an async function or not and just have to make 5 nested callback functions or awaits to get a synchronous program to work
  • 2
    @alexbrooklyn Any good sources on await? I still can't figure out how to use that right 😬
  • 5
    @highlight
    (async () => {

    let a = await async_fn1();
    let b = await async_fn1();
    ...

    })();
  • 3
  • 7
    Because the browser still needs to be able to render all your cat or fox gifs while executing javascript, or rather "executing javascript". So if you're doing something like waiting for resources, waiting a couple of ms to play an animation, drawing a loading bar, and more stuff then you wouldn't wanna give your users a bad experience by freezing the page. That's why you need it.

    As for how to get it, well I used to feel that way too, the whole "durr this shit is so difficult" whenever I read about it. Then I actually tried it out, played around with it, and found to my shock and horror that it wasn't so difficult after all.

    Try something like axios, and instead of actually copy pasting the code examples write it out. https://github.com/axios/axios
  • 0
    This is what gives JS such a bad rep. But try doing the same thing in some other lang. JS can handle async hell although it is not easy. Which other lang can?
  • 2
    @Ubbe I do async stuff all the time in C++ now. Any communication with external resources requires some kind of callback system. You could spin off threads for intensive stuff, but you are still stuck doing some kind of communication to that thread. Async is here to stay AND will INCREASE as apps move to more cores. The libraries I use now require async approaches anyway.

    Async isn't going to get better.
  • 0
    @inaba While I agree with that, this is a node CLI application where the process has to wait for the result regardless.

    It would be nice if it would be an option to do it synchronous or something like that
  • 0
    @linuxxx if you go ham with await you can basically force your way through it, wont be pretty tho'.
  • 0
    @linuxxx since node is build on top of V8 you still have the async nature of it all. It's also built with http servers in mind to which having everything be blocking by default isn't very nice. Having the time and callback in setTimeout be swapped wouldn't be a bad thing as well but hell I'll live with it. And tbh once you get used to using async and await it's really really nice, especially later on.
  • 0
    @linuxxx And what would be the difference between

    try { await asyncX() } catch(err){ }

    and

    try { synchronousX() } catch(err){ }

    except for the obvious part that it would totally block the main thread?
  • 1
    @hitko everything nothing at all, it depends on context. You're using it in your CLI app? Fuck all. You using it in an app with multiple things happening? Everything
  • 0
    There are pretty good talks about async/await and promises in JS, which helps to understand the concepts/what they are ment for (since, they're different from example async stuff/threads in C)
  • 1
    Don't forget your catchs after your thens 😎
  • 1
    @olback that sounds just like fancy good old
  • 0
  • 0
    @Wack yep, only difference being support for await.
  • 0
    So... Async/Await (and promises) can be (in theory) of the main thread, while I think `window.setTimeout()` is on the main thread. Any other differences?
  • 1
    const result = await new Promise(async (resolve, reject): Promise<any> => someSyncFunc(params, (err, val) => err ? reject(err) : resolve(val)));

    await new Promise((resolve, reject): any => {
    try {
    resolve();
    } catch (e) {
    reject(e);
    }
    });

    .then is ugly as shit
  • 0
    If your gonna learn Node/JS, should learn about the event loop and the difference between single thread and multithread languages.

    My team building JS APIs... but after years still don't understand this and their code in a barely JS way because they never fcking understood how it works.

    I'm actually stuck trying to fix a piece of code now because it somehow ended up hanging the server... Which only happens if there's a memory leak.... Or something fcking blocked the event loop/thread...
  • 2
    @billgates really javascript becomes so much clearer once you learn bout the event lop and the event queue and how they relate to the call stack and how the singlethreadedness of the language is actually kinda misleading
  • 0
    @inaba well the q can't process of the loop can't run?

    My issue is there server is accepting connections but doing nothing with them and I think that's because the either thread that's actually processing the q is stuck on a single task
  • 0
    @billgates the queue can't really do anything unless the call stack is empty and the loop isn't blocked (which it usually is by there being something on the call stack). But tbh the que just saves information about functions (callbacks) that can be run whenever the call stack is empty
  • 0
    @linuxxx if it makes you feel any better I'm a "senior-level" programmer and use async/await/promises every single day and still fuck them up every time and have to relearn it every day. Not sure how I picked up on machine learning faster than async-await haha.
  • 1
    I see what you're saying. I like JavaScript but I think it's a disaster server side. Async on top of framework-hell, npm packages and no reliable MVC web framework whatsoever.

    Just use PHP, or Python + Flask/Django. I hope NodeJS improves in the future. I think it's just going through growing pains as of now.
Add Comment