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
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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.