A Javascript Promise Example to Help You Out

Promise provides a convenient way to organize asynchronous code. In modern JavaScript, promises are often used, including implicitly, using generators. Promise is a special object that contains its state. First, it is pending, then it is either fulfilled (successful) or rejected (“done with an error”). On Promise, you can hang a callback of two types: onFulfilled, which is triggered when the promise is “successful,” and onRejected, which is triggered when the promise is in the “executed with an error” state. The method of using a promise is described in the following Javascript Promise example.

If you need to know how to use Javascript promises, then our sample will be helpful for you. Having read this example, you will be able to do your own assignment much faster and easier. At the end of completing your task, you will get the result you need. You can also find other samples on our site for different disciplines. The following example will be appropriate for the IT discipline.

Usage of Promises in JavaScript. Part 1

Let’s talk about promises in JavaScript, and how to use them correctly, showing several patterns of their application.

Using promises

If you use a third-party library that already supports promises, it’s quite easy to use them.

Namely, you need to pay attention to two functions: then () and catch (). For example, we have an API with three methods: getOrder (), updateOrder (), and deleteOrder (), each of which returns a promise:

javascript promise examplejavascript promise example completed

 

Each call then () creates the next step in the chain of promises. If an error occurs anywhere in the chain, a catch () block is called, which is located behind the failed portion. The then () and catch () methods can either return a value or a new promise, and the result will be passed to the next then () operator in the chain.

Callbacks to promises

One of the first techniques, which is useful to learn when switching from a callback to promises, is the conversion of callbacks to promises. The need for this can arise if, for example, you are working with a library that still uses callbacks, or with its own code written with their application. It is not so difficult to go from callbacks to promises. Here is an example of converting a function called Node fs.readFile, based on callbacks, to a function that enables promises:

javascript promise example solved

The main thing in this function is the Promise constructor. It takes a function that, in turn, has two parameters – resolve and reject, which are also functions. Inside of this function, all the work is performed, and when we terminate, we call resolve in case of success, and reject if there was an error.

Values to promises

In ES6 there are a couple of convenient helper functions for creating promises from normal values. This is Promise.resolve () and Promise.reject (). For example, you might have a function that needs to return a promise, but which handles some cases synchronously:

javascript promise example analyzed

Note that you can pass anything (or nothing) when calling Promise.reject (), however, it is recommended that you always pass the Error object to this method.

Simultaneous execution of promises

Promise.all () is a convenient method for concurrently executing an array of promises. For example, say we have a list of files that we want to read from the disk. Using the previously created function readFilePromise, the solution to this problem might look like this:

javascript promise example

Leave a Reply

Your email address will not be published. Required fields are marked *

Customer testimonials

Submit your instructions to the experts without charge.