Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
Woah woah woah, I understand the hate around using .then but don't talk bad about my man .catch, man
-
h3rp1d3v5232yWhat about 5 nested .subscript() with nested .then()? Any special place for these angular people?
-
@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 -
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. -
@vintprox (unless i especially expect a function to fail when any operation inside fails)
-
@rantsauce if the parameter function for then is re-used many times, please make it it's own function, and await both of them.
-
@Grumm that means you are using .then and .catch
(P.s: like all javascript devs, we will update hell's dependency "heaven") -
msdsk31822yWhy are you nesting .then()? You specifically should not be nesting them, if you're nesting them something is wrong with your code design.
-
@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? -
Grumm18112y@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 });
});
});
} -
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)
-
@black-kite you should be able to pass a flag.
Also if something fails why don't you let it crash (after logging ofc) -
@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". -
@vintprox yes thank you!!! The project I was thrown into did not that implemented!! Will definitely be adding it to the code
-
Grumm18112y@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. -
msdsk31822y
-
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})); -
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.
-
@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.
Related Rants
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
rant
await
promises
js
async
javascript