javascript

How to use Promise.then() in JavaScript


The then() method returns a Promise. It takes up to two arguments: callback functions for the success and failure cases of the Promise. Promises can be chained one after the other using the .then() chain.

The subsequent promises in .then chain are only executed once the current promise resolves successfully. If the current promise rejects with an error, all subsequent promises which are chained by .then are skipped and the first .catch is executed.

Syntax:

Promise.then(onFulfilled[, onRejected])

Example:

const promise1 = new Promise((resolve, reject) => {
  resolve('Success!');
});
promise1.then((value) => {
  console.log(value);
  // expected output: "Success!"
}, (error) => {
  console.log( error);
});

Share on social media

//