web analytics
Select Page

Pizza Promises

pizzaA fourth-grade student has a Math test every Wednesday morning. The teacher grades the tests on her lunch break and returns the graded tests to the students in the afternoon, so they can bring the results home to their parents every Wednesday after school. The student’s father makes a deal with his daughter every Wednesday morning before school: He tells her every that if she returns home that day with an A on her Math test, he will order pizza for dinner that evening. If she fails to get an A, then he would instead prepare a regular old meal for dinner – no pizza.

One Wednesday, the daughter returns home from school with her Math test in hand. She received an A, so that same evening, her father ordered pizza. The promise was fulfilled.

The following Wednesday, the girl hands her father her latest Math test. When the father sees the B grade in the top right corner of her test, he begins to prepare broccoli and rice for dinner. The promise was fulfilled.

math-testThe next week, the daughter came home empty-handed. She had lost her test on the school bus. As a result, the father did not order pizza, as the promise was rejected.

This is an example of promises – Pizza promises, I suppose. JavaScript promises work much in the same way. In the example above, the father is controlling the logic when he tells his daughter every Wednesday morning to bring back her Math test after school. Then, once the father has received the requested information later that day, he fulfills his promise. When he does not receive the requested information, the promise is rejected. And on Wednesdays while the girl is at school, the promise is pending – the operation that will determine the value is in execution or has yet to be executed.

These are the three promise states: fulfilled, rejected, and pending. Those states change with the execution of different operations.

 

Why use promises?

Promises have been growing in popularity among developers as they provide an easier-to-use alternative for traditional use of callback functions in handling and executing asynchronous operations. They offer better error handling mechanisms on asynchronous call failures that resemble widely used try/catch features. As well, they can be chained and grouped and work in cases where even Events fail. Promises make complex async code look and ‘feel’ more simplified, can queue multiple API calls or asynchronous actions, and work best in both parallel processing as well as in sequencing operations. Additionally, they are much easier to read and understand even for beginners and are more ‘natural’ in their look and feel.

 

How to create a promise

Rsvp.js is the most widely used library when it comes to creating and using JavaScript promises. Using a ‘revealing constructor’- one that takes a single parameter with two argument ‘reject’ , ‘fulfil’- it assigns the respective value to the newly hatched promise.

i.e.  var promise = new RSVP.Promise( function(fulfill, reject )

{  //do all that          //creates a new Promise Object and initializes it

});

If we are to change the state of the promise object using our own asynchronous operations, we can make use of then method, which has both success and failure handler functions and the selection of the two handlers rests on object’s initial resolution.

i.e. prom.then(onFulfilled, onRejected);

 

Share This