-
Book Overview & Buying
-
Table Of Contents
JavaScript from Frontend to Backend
By :
Promises are another way to use callback functions. Rather than integrating the callback function into the method call (as a parameter), we use it as a parameter of the new then(callback) method. This simplifies the reading of JavaScript code in case it uses callback functions.
For an object to use the then(callback) method, it must be a Promise class object. The Promise class is a class defined in JavaScript language.
The Promise Class
A Promise class object uses a callback function of the form callback(resolve, reject) as a parameter of its constructor.
The resolve and reject parameters are functions, which will be called from the promise’s callback:
resolve() function is called, it triggers the then(callback) method.reject() function is called, it triggers the catch(callback) method.The resolve() function must be called, otherwise the then(callback) method cannot be executed. On the other hand, calling the...