Reader small image

You're reading from  JavaScript from Frontend to Backend

Product typeBook
Published inJul 2022
Reading LevelBeginner
PublisherPackt
ISBN-139781801070317
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Eric Sarrion
Eric Sarrion
author image
Eric Sarrion

Eric Sarrion is a trainer and a developer as an independent consultant. He has been involved in all kinds of IT projects for over 30 years. He is also a long time author in web development technologies and is renowned for the clarity of his explanations and examples.
Read more about Eric Sarrion

Right arrow

Using promises

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:

  • When the resolve() function is called, it triggers the then(callback) method.
  • When the 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...

lock icon
The rest of the page is locked
Previous PageNext Chapter
You have been reading a chapter from
JavaScript from Frontend to Backend
Published in: Jul 2022Publisher: PacktISBN-13: 9781801070317

Author (1)

author image
Eric Sarrion

Eric Sarrion is a trainer and a developer as an independent consultant. He has been involved in all kinds of IT projects for over 30 years. He is also a long time author in web development technologies and is renowned for the clarity of his explanations and examples.
Read more about Eric Sarrion