Chapter 3. The Promise Paradigm

In this chapter, we will focus on what the promises paradigm is, from where it originated, how languages implement it, and what problems it can solve for us.

We have briefly discussed the origin of the promise pattern in Chapter 1, Promises.js. In this chapter, we will explore this subject in more detail, in a generic way, so that it would clear the logic and theory behind the adoption of promise in different languages and in particular, how it's helping us in today's modern programming.

Callback, revisited

In previous chapters, you learned how the JavaScript mechanism works. The single-threaded model of JavaScript has its limitation, which can be controlled through better use of callbacks. However, the scenarios such as callback hell really pushed engineers to find and implement a better way to control the callbacks and maximize the performance of the program, while staying inside a single thread. A callback is a function that can be passed as an argument to another function to be executed when it's called.

There is absolutely no harm in using callbacks, but there are also a number of other options available to handle asynchronous events. Promise is one such way to handle asynchronous events and has more efficiency than many of other asynchronous tools in its family.

To understand more clearly why we needed to implement Promises.js in asynchronous programming, we need to understand the concept behind the promise and deferred objects.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.55.14