Promises In Simple Language
"Can you take the trash out before I get back?"
"...I Promise - I will take the trash out before you get back to the apartment."
"What are the things we should prepare for the team while I'm away?"
I Promise - I will get back to you on how we can plan for your time away from the office."
Between Two Parties
The Real World
In The Real World, promises are between the promise-requester and the promise-maker.
The one who makes the promise to take the trash out is the promise-maker. The promise-maker makes the promise to the other party.
The one who waits for the result of the promise is the promise-requester.
The Promise-requester requests the promise from the other party.
During the time that the promise is either being completed or not, the "thing" that the requester has is the promise itself (maybe a bit abstract here).
During the time that the promise is either being completed or not, the promise-maker is doing all of the work related to the promise.
In Code
In JavaScript, the two parties might be a little more "abstract". The promise-maker, in code, is the Promise
feature in JavaScript. The Promise
feature in JavaScript will do all of the work related to the promise.
In JavaScript, the process/service/program being run is the promise-requester.
I, as a JS writer, write into my program a request for a promise to be fulfilled. JS, itself, has the built-in Promise
utility to take care of the work.
Promises Have 3 "States"
JavaScript implements 3 "states" of a promise - perhaps like the real-world.
Pending
Promises in a pending state are not completed. When I've made a promise to take the trash out while my roomate is gone, the promise is pending until my roomate comes back and finds out the result of the promise.
Fulfilled
Perhaps this is the ideal - a promise, after pending, gets completed!
In JS, the promise returns any information requested, as well.
Rejected
Sometimes promises are incomplete. The Promise
mechanism provides this rejected
state option.