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…

The post How to use Promise.then() in JavaScript appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Deven

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);
});

The post How to use Promise.then() in JavaScript appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Deven


Print Share Comment Cite Upload Translate Updates
APA

Deven | Sciencx (2021-10-15T11:56:47+00:00) How to use Promise.then() in JavaScript. Retrieved from https://www.scien.cx/2021/10/15/how-to-use-promise-then-in-javascript/

MLA
" » How to use Promise.then() in JavaScript." Deven | Sciencx - Friday October 15, 2021, https://www.scien.cx/2021/10/15/how-to-use-promise-then-in-javascript/
HARVARD
Deven | Sciencx Friday October 15, 2021 » How to use Promise.then() in JavaScript., viewed ,<https://www.scien.cx/2021/10/15/how-to-use-promise-then-in-javascript/>
VANCOUVER
Deven | Sciencx - » How to use Promise.then() in JavaScript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/15/how-to-use-promise-then-in-javascript/
CHICAGO
" » How to use Promise.then() in JavaScript." Deven | Sciencx - Accessed . https://www.scien.cx/2021/10/15/how-to-use-promise-then-in-javascript/
IEEE
" » How to use Promise.then() in JavaScript." Deven | Sciencx [Online]. Available: https://www.scien.cx/2021/10/15/how-to-use-promise-then-in-javascript/. [Accessed: ]
rf:citation
» How to use Promise.then() in JavaScript | Deven | Sciencx | https://www.scien.cx/2021/10/15/how-to-use-promise-then-in-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.