How to better JavaScript with promises?

You may have already heard the opinion that the future of JavaScript is in promises. All the great dynamic fighters are already at home in them. Have you also discovered what makes them special? Isn’t it enough to just use some callback? Are they really that extremely important? In this article, you’ll learn what promises are and how to write better JavaScript with them.

What exactly are promises?
Promise and Deferred are long-known programming constructs, where promise expresses a value that is not yet known, and deferred expresses work that is not yet completed.

Each deferred job has its “promise” of value (promise), which in turn has handlers that say what to do qatar phone number data when the job is completed and (or) the value of the promise is known, and further has three states etc.

Promises are more understandable
Let’s say you want to grab some data from the HipsterJesus API and add it to your site. This API corresponds to data that looks like this:

Using the callback, we would write something like this:

phone number data

If you’re experienced with jQuery, you’ll recognize that we make a GET request and expect JSON (JavaScript Object Notation, a platform-independent data format designed for data transfer) in the body of the response. We also pass a callback function that takes the JSON response and adds it to the document.

All kinds of ways are invented to

peed up the data transfer at least a little. Based on the above, it looks like the donors – what to pay attention to when latency could be solved by reducing the number of requests. This reduction can be achieved by ingesting all page components into HTML. As bw lists you know, practically everything fits into HTML. Not only CSS, JavaScript, SVG, but thanks to the data pseudo-protocol: also raster images. The file is first encoded in base64.

Scroll to Top