Javascript Promise

Hey :smiling_face_with_three_hearts:,
I can’t see what’s the utility of the Promise, could someone please provide a practical example?

Hey there,

there are lessons on Freecodecamp on this.

The first sentence is:

A promise in JavaScript is exactly what it sounds like - you use it to make a promise to do something, usually asynchronously.

You make the promise to your parents, that you will learn coding.
Then their lives go on and they do their stuff.
After some time you come back to them and either go like “Yes, I did it!” (fulfill the promise) or “No, I didn’t.” (reject the promise).

They didn’t stop their lives and waited for you (sync),
you made a promise and worked in the background (async).

1 Like

A real-world analogy i use often is the case of a restaurant. When the kitchen gets your order slip, that is a Promise. At some future time, it will resolve (and the wait staff will bring your food) or reject (and you’ll be informed that they’re our of tofu).

One of the most common uses for Promises has been retrieving remote data. Rather than leave your script hanging till the data arrives, with Promises we can do other things whilewe wait.

2 Likes

The internet has lots of documentation about promises.

Is there a particular problem you are trying to solve with promises and you are not able to find solution for?

1 Like

Thank you for the analogy, it’s so helpful.