r/badcode • u/i_will_yeet biggus dickus • Nov 10 '21
js the equivalent async/await version is so much cleaner than this callback hell
406
Upvotes
r/badcode • u/i_will_yeet biggus dickus • Nov 10 '21
132
u/Zeppelin2 Nov 10 '21
And for my next trick, I’ll make your error handling magically disappear!
111
u/memes0192837465 Nov 10 '21
The left isn’t bad code. It’s just older code.
34
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Nov 10 '21
Image Transcription: GitHub Commit Summary
[The screenshot shows a GitHub commit summary on
index.js, with the old lines to the left and the new lines to the right. The old lines are as follows:][The new lines are as follows:]
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
6
u/bistr-o-math Nov 10 '21
This is bullshit. Left hand side is not even working code. Variable
profilehas never been declared or assigned a value. Line 1011
u/dytigas Nov 10 '21
This is the same as moving from the java 6 world of Android to kotlin coroutines. It's a magical time your career
4
u/codewrangler315 Nov 11 '21
The async await code should name their functions appropriately aka if findUserByName returns a user ID then it should be named getUserIdByName(name) or wrap it in an object user.byName(name)
6
u/philogy Nov 10 '21
Destructuring
accountwould've made the code on the right a tad better ;PEDIT: and
profile1
u/_PM_ME_PANGOLINS_ Nov 10 '21
It's not that bad. Back in the day you'd probably one-line the error checks too.
1
u/rjlin_thk usinged Nov 10 '21
what happens if findbyusername isnt success, just asking
1
u/[deleted] Nov 10 '21
Try/catch, no?
-3
u/Willinton06 Nov 10 '21
You can double post this in r/goodcode
-13
u/[deleted] Nov 10 '21
I don't really see how the one on the left is bad code.
This seems like a refactor at best.
I'm also not fond of async await.
-23
u/Tvde1 Nov 10 '21
Still, 3 calls???
0
u/[deleted] Nov 10 '21
[deleted] — view deleted comment