Introduction to JavaScript Promises.
In JavaScript, among many ES6 features like template literal, deconstructing, or arrow functions we were introduced to Promises. Some love them and some not so much. Promises are designed to support asynchronous programming and they simply replace callbacks functions. As a result, asynchronous programming is simplified and visually not only looks more attractive to an eye but also is much more efficient. In today’s blog, we will learn what is Promise, what are the states Promise can take, how Promise is created, and what methods we can use when creating Promise.
What is Promise? It is an object that is a placeholder for future value, with possible three outcomes of an asynchronous execution.
What are the three states Promise can be in:
Pending: in these state when Promise is not completed yet, first state.
Fulfilled: in this state Promise has been successfully completed. Promise has value of resolved.
Rejected: these sate happens when operation failed. The outcome of this operation will result with an Error, indicating the rejection.
Correct terminology for fulfilled Promise is settled, moment when Promise is delivered.
Let’s think of real-life example, think of doing the laundry. What will be a pending, fulfilled or rejected state of actions. Lets break it down:
Pending: would be while your machine is on and running but not yet finished.
Rejected: would be while we encounter some sort of issue, like you did not put any detergent, and in the end the cloths are still dirty. The entire operation will have to be repeated.
Fulfilled — when everything went as planed and cloths are clean after action is completed.
How do we create and Promise?
As mentioned earlier a Promise is an object, and to create a Promise object we will use new keyword and the Promise constructor method.
In Promise constructor method takes a function as its only parameter function myFunc, this function runs automatically when constructor is invoked.
The above example myFunc takes two parameters:
Get Sabina S Ohlsen’s stories in your inbox
Join Medium for free to get updates from this writer.
resolve is a function that takes one argument, this function will change a Promise status.
reject is a function that takes reason or an error as an argument. This function will change status of the Promise from pending to reject, and the reason will be set to the argument passed into reject().
Let’s deconstruct above code example, first newPromise that is constructed as a new Promise; which essentially is the Promise constructed method.
myFunc() is passed to the constructor and performs two functions as parameter: resolved and reject.
When condition is evaluated to true, then resolve() function is invoked ‘Cindy passed the exam!’
But when condition evaluates to false, then reject() function gets invoked ‘Cindy failed the exam.’
Let’s look at another example to see pending status of the Promise. For this example we will wrap the code of the executor in the setTimeout() function.
In this example we start the Promise at pending state. Its value is equal to undefined and the Promise value will return after Promise is completed. And after it is completed, value will change from pending to completed.
But if we change value of variable reading form true to false, Promise gets rejected after 5 seconds, the state of the Promise changes from pending to rejected.
For Promise objects with pending status, we need to schedule a callback function. When Promise is ready, it will be resolved or rejected. We can to that with the Promise methods: then() and catch().
then() — is used when Promise is successful, it takes two callback functions. They are called handler. The first handler will be called when promise is resolved and second handler when Promise is rejected.
catch() — this method is handling the rejection, as you can see in above example, second handler is handling rejection, but .catch() is another way to do the same.
In this short introduction to Promises you can get a feeling how much more flexible you can be and improve your asynchronous code and have better control flow. No more nested callback function, your code is much more attractive, easy to read, and efficient.
Thank you for reading!










