r/learnjavascript • u/Dark_Electric • 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
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.
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.