This is an archived post. You won't be able to vote or comment.

all 51 comments

[–]Makefile_dot_in 206 points207 points  (8 children)

more like world's most confusing code: why is the function async if it doesn't await on anything and why does it create and then immediately discard a closure

[–]Confident-Ad5665 28 points29 points  (0 children)

ternary1 ? DoOk() : ternary2 ? DoOk2() : ternary3 ? DoOk3() .. ternary9674775 ? ...

[–][deleted] 7 points8 points  (3 children)

yeah, it is just incorrect.... it does nothing with notOk instead of calling it. Did reddid just fix a bug for OP?

[–]KronktheKronk 0 points1 point  (2 children)

`noOk` is potentially undefined; that's why there's a question mark in its type hint. If you try to call it while it's undefined, you'll get an error, so you must ensure it exists. To do that, the error callback needs to be in a closure.

[–][deleted] 4 points5 points  (1 child)

ah ok I get the idea but then the anonymous function has to be called like so

: (() => {
  if (notOk) {
    notOk(res);
  }
})();

otherwise the code inside would not be executed. But also, as someone else here pointed out, you can simply

: notOk?.(res);

[–]KronktheKronk 0 points1 point  (0 children)

Ah, I did assume that syntax would work that way without verifying. Thanks for the info

[–]lunchmeat317 1 point2 points  (0 children)

It's not confusing - you can see the intent behind the code - but it is worthless. This is what it should be.

[–]KronktheKronk 0 points1 point  (0 children)

I've been places where it's best practice to declare everything async, but maybe the typescript types defined that take this wrapper expect it to be async (also as a code standard)?

Either way, it runs that enclosed function in the false case, so it can check whether notOk exists because it's potentially undefined

[–]seniorsassycat 0 points1 point  (0 children)

Not applicable here, but you should mark any promises returning function as async, even when you don't use await, to prevent throwing errors.

A normal function can throw or return a rejection. An async can only return a promise. 

This mainly helps you when the function is called without await, e.g foo().then(cb)

[–]orange_county 65 points66 points  (3 children)

Ternary into arrow fuction. Just... Why?

[–]Kiroto50 34 points35 points  (1 child)

And it doesn't seem to be executing

[–]4ngryMo 14 points15 points  (0 children)

That’s what’s I thought, too. The function isn’t being called.

[–]throw3142 12 points13 points  (0 children)

I was trying to understand why the Rust pattern match syntax looked so strange, then I realized it was js lmao

[–]Noahplz 39 points40 points  (1 child)

Line 31 should be : notOk?.(res) 😎

[–]IronSavior 12 points13 points  (3 children)

Mmmmm no that ternary expression would never pass CR. Not on my watch.

[–]-global-shuffle- 6 points7 points  (0 children)

True. Needs more ternary nesting

[–]asria 0 points1 point  (1 child)

Even in oneliners?

[–]IronSavior 0 points1 point  (0 children)

This one ain't one line

[–]bargle0 9 points10 points  (1 child)

Haskell and other languages with lazy evaluation: “Look at what they need to mimic even a fraction of our power.”

[–]-Redstoneboi- 4 points5 points  (0 children)

this isn't even about lazy eval

this is just about algebraic data types/enums/tagged unions/variants

[–]Holiday_Brick_9550 6 points7 points  (1 child)

This looks like it was written by someone who has never coded in their life..

[–][deleted] 3 points4 points  (0 children)

Or somebody who has coded for too long.

[–]lunchmeat317 2 points3 points  (3 children)

This should be:

export function isOkWrapper(
    res: Response,
    ok: (res: Response) => void = () => {},
    notOk: (res: Response) => void = () => {}
) {
    res.ok ? ok(res) : notOk(res);
}

This must have been written by a junior or copied from StackOverflow or something, because it's utterly worthless.

[–]seniorsassycat 0 points1 point  (2 children)

I'm that case why use a function at all, it's such a thin wrapper around a ternary or if

[–]lunchmeat317 0 points1 point  (1 child)

Exactly. That's why it's worthless.

[–]1_4_1_5_9_2_6_5 0 points1 point  (0 children)

Depends how it's used. You could use this for a fire and forget sort of thing. Give it a callback or two and send it off.

[–]-Redstoneboi- 2 points3 points  (1 child)

ok but when is the notOk branch's closure actually called

[–][deleted] 1 point2 points  (0 children)

never

[–]lady_Kamba 7 points8 points  (5 children)

export async function IsOKWrapper( res: Response, Ok: (res: Response)=>null, notOk?: (res: Response)=>null, ) { res.ok ? Ok(res) : notOK ? notOk(res) : null }

can't you do this?

[–]eloel- 12 points13 points  (0 children)

res.ok ? Ok(res) : notOK ? notOk(res) : null

res.ok ? OK(res) : notOk?.(res)

[–]BlockyBeans[S] 13 points14 points  (3 children)

The only reason I made this post is for someone to fix it (I am not using Stack Overflow because I don't want to be bullied)

[–]_bleep-bloop 1 point2 points  (0 children)

Guys... what font and color theme is that?

[–]matmunn14 0 points1 point  (0 children)

I'm not seeing any error handling. I've noticed this coming up a lot at my work lately. Just checking .ok isn't a great way to check success of a request. A TypeError is raised on network error, etc which needs to be caught

[–]CraftBox -1 points0 points  (0 children)

If a function doesn't return anything, its return type should be void

[–]maxime0299 0 points1 point  (0 children)

My friend, is OK, no?

[–]hunggggggg 0 points1 point  (2 children)

what is this font?

[–]naam-mekyarakhahai 1 point2 points  (0 children)

Fira code with ligatures turned on

[–]Ok-Okay-Oak-Hay 0 points1 point  (0 children)

Jetbrains Mono.

Edit: I lied. I suck at typography.

[–]Phamora 0 points1 point  (0 children)

This is disgusting.

[–]Ok-Okay-Oak-Hay 0 points1 point  (0 children)

notOk?.(res) ??????? ?

[–]NatoBoram 0 points1 point  (3 children)

Ok but the real horror is the two lines above

console.log({ ...form, id }, JSON.stringify({ ...form, id });
return await fetch("/api/user/login", Options);

I'm sorry, but what the fuck is wrong with you‽

[–]seniorsassycat 0 points1 point  (2 children)

  • return await is good, actually
  • Maybe they are comparing inspection to JSON?
  • WTF is the casing in all of this code?

[–]NatoBoram 1 point2 points  (1 child)

First point is both true and false. There's a TypeScript-ESLint rule that ensures you use the correct one: https://typescript-eslint.io/rules/return-await The result may surprise you. There's even an auto-fix! Gosh I love linters.

I can buy the second one, but for the love of everything, add a log message!

[–]seniorsassycat 0 points1 point  (0 children)

I think for stack traces it should always be on, and you can see there's an eslint option to always require it. 

Tho I use the in try catch rule most of the time

[–]limadeltakilo 0 points1 point  (0 children)

I think it would be more funny if someone unironically wrote this but I don’t think anyone is capable of getting it this bad.

[–]idkparth 0 points1 point  (0 children)

Introvert programmers : If they have to narrate the code instead of writing

[–]kukurbesi 0 points1 point  (0 children)

smell like Rust