r/javascript • • Mar 03 '18

A good write up on callback hell... we've been dealing with this at work a lot lately after switching to react...

https://medium.com/@js_tut/the-great-escape-from-callback-hell-3006fa2c82e?T
2 Upvotes

4 comments sorted by

10

u/name_was_taken Mar 03 '18

They're making the common mistake of await-ing asynchronous values before they're actually needed, turning what should be asynchronous code into synchronous code.

const a = await $.get('a');
const b = await $.get('b');
console.log(a, b);

should be

const a = $.get('a');
const b = $.get('b');
console.log(await a, await b);

6

u/cspotcode Mar 04 '18

I'd recommend Promise.all so you get earlier termination if b throws an error before a resolves. (it's pedantic but hopefully helpful to those reading)

6

u/azium Mar 03 '18

Something is lost between 7. and 8. Why not just write promises properly instead of going to some unusual techniques?

Anyways. OP what exactly are you dealing with more when you going to React?

4

u/[deleted] Mar 04 '18

Yeah. I was wondering WTF happened. They ended up in promise hell because they used promises wrong... I’m so confused by this article.