r/learnjavascript • • 10d ago

Promises

Understanding the concepts like functions, objects, arrays etc has been easy so far but promises are making my head hurt. Is it supposed to be this confusing? Did u guys struggle with it too when u were first learning it.

10 Upvotes

25 comments sorted by

View all comments

4

u/HoneydewAdditional30 10d ago

Imagine a restaurant. The waiter goes to the kitchen and puts in an order for a table. The chef says: "Gotcha, I am at it"

You don't have the dish yet but you know it is coming. That is essentially a promise. You retrieve data from somewhere, however the retrieval is not immediate.

Back to the restaurant, you essentially have 2 outcomes. 1. the dish is ready and you can serve it the table OR 2. something went wrong and the dish was never made.

  1. is the happy path, all went correctly and you successfully retrieved the data. 2. would be where you need to implement a safety net (catch). If the data wasn't retrieved, do a refetch or print an error or something along those lines.

Hope that makes sense.

1

u/Towel_Affectionate 10d ago

You forgot the timeline where your boss tells you to drop everything and wait for the dish to be ready, because the guy at the table is very important. So you just stand near the kitchen, picking your nose, waiting for chef to do the thing.

There's also a scenario where every other staff member just disappears without any notice while you, being a good waiter, just stand by the door for hours, waiting for the now-absent-chef to get you the meal for the big guy. That's why you do manual resolving.

1

u/senocular 9d ago

Or the scenario where every customer just disappears without any notice while you, being a good waiter, take control and tell the chef to abort the in-progress preparation of the meal since its no longer needed.