4

There's a special place in hell for JS people using `.then()` and `.catch()` instead of simply `await func()`.
Why have 5 lines of code with an await, when instead you can have 5 nested `.then` callbacks.

And yes I know there are some cases where async/await isn't applicable, but that's rather rare

Comments
  • 9
    you could abbreviate this rant:

    there's a special place in hell for JS people.
  • 1
    Woah woah woah, I understand the hate around using .then but don't talk bad about my man .catch, man
  • 2
    What about 5 nested .subscript() with nested .then()? Any special place for these angular people?
  • 0
    @h3rp1d3v i haven't tried angular but .then **inside** the subscript function sounded like you may be able to use a async function invocation there.

    Define the function as async, do stuff with cleanish code, and only give "functionz()" to the .subscript
  • 1
    Well, "then" is avoidable, I agree.

    But why do you hate on "catch"? If someone DOESN'T properly catch the rejection at some point and leaves it "as is", there is a special cauldron in hell for THEM.

    Of course I wish there was a prettier syntax for catching other than ".catch(...)" or "try..catch", but JS is already bloated to such point that I don't care.
  • 1
    @vintprox (unless i especially expect a function to fail when any operation inside fails)
  • 1
    @melezorus34 yeaaah, that's a private situation... Let's leave it to tests.
  • 0
    await then() catch()?
  • 0
    @rantsauce if the parameter function for then is re-used many times, please make it it's own function, and await both of them.
  • 0
    Just add Promise everywhere and you will be fine (Still going to hell 🤷‍♂️)
  • 1
    @Grumm that means you are using .then and .catch

    (P.s: like all javascript devs, we will update hell's dependency "heaven")
  • 2
    Why are you nesting .then()? You specifically should not be nesting them, if you're nesting them something is wrong with your code design.
  • 3
    @msdsk you have a really good point:

    The promise functions (whether it be the contructor or the then/catch parameter) are by default sync. You can't await them.

    So the only place you can use .then in a meaningful way is returning it, which is really fucking dumb because you already have a semantic way to SERIALIZE (.then), why do you have to do the callback hell method in a different blue?
  • 0
    @melezorus34 to counter the async factor of then() here is an example : (devrant will break the formatting sorry for that)

    function getData() {

    fetch('https://random/api/image/random')

    .then((response) => response.json())

    .then((response) => {

    doOtherAsyncThing().then((otherResponse) => {

    // do stuff with `response` and `otherResponse`

    console.log({ response, otherResponse });

    });

    });

    }
  • 0
    Probably bad design on my part but sometimes in NodeJS I use self invoking async functions. I have to use .catch() on them to prevent unhandled errors from crashing the server (and also for logging purposes)
  • 2
    @black-kite There is process.on('unhandledRejection', ...)

    Why?
  • 2
    @Grumm
    Bro... Promise.all is a thing...
  • 2
    @black-kite you should be able to pass a flag.

    Also if something fails why don't you let it crash (after logging ofc)
  • 1
    @melezorus34 Yes, also there is "fail fast" approach which helps in a long term. "Just let it crash, man!" 😉 I mean, it's totally good when you don't want side effects doing something out of ordinary.

    Unironically, I like how Java handles it on the language level - static analysis will warn the programmer when something isn't handled when it should be or else suggest to add "throws".
  • 1
    @vintprox yes thank you!!! The project I was thrown into did not that implemented!! Will definitely be adding it to the code
  • 1
    @melezorus34 not if you need data from a previous result. It is a bad example. You are better of using await.

    But nested then() is a thing and can be a pain to look at.
  • 0
    @Grumm

    doSomethingAsync().then(res=>doSomethingElse(res)).then(res=>youCanChainItLikeThis()) ???
  • 0
    Bro you chain it wrong, you had to do
    doSomethingAsync().then(doSomethingElse).then(youCanChainItLikeThis)

    Also in their example two different promises were used, so if anything it's "easier" this way:

    const first = getRandomPic();
    const second = first.then(doSomethingElse);

    Promise.all([first, second])
    .then(([f,s])=>console.log({f,s}));
  • 0
    because nesting and chaining functions sent to functions as callbacks to call callbacks from functions as functions with callbacks and callback functions and function callbacks is The Proper JS Way to do things.
  • 0
    @tosensei that.

    If not, sometimes it comes from code refactoring from old JQuery ajax calls and if you suimple use data = await func(), you can have variables scope conflicts, which ofc JS will not tell you.
Add Comment