Chaining promises and error handling

Chaining promises and error handling

Promise chaining in JavaScript programming allows programmers to execute multiple asynchronous programming operations in multiple sequences, where error management can be customized or managed in this process. So let’s learn how to chain the promise process in a program in JavaScript, and effectively manage and control the errors occurring in the program. So that in the current program it is ensured that all program errors are properly caught in the promise chain.

Chaining promises and error handling

Chaining Promises in JavaScript.

When a programmer in a JavaScript program has to perform multiple asynchronous tasks simultaneously, which need to be executed in sequence, then the programmer can chain multiple .then() call methods to display. Where each .then() function method inputs the result of the previous promise, and returns a value or another promise output as output.

Basic JavaScript Promise Chaining.

So let’s learn the process of chaining multiple promises in JavaScript.

const process1 = new Promise((resolve, reject) => {

setTimeout(() => {

resolve(“Process 1 is done”);

}, 2000);

});

const process2 = new Promise((resolve, reject) => {

setTimeout(() => {

resolve(“Process 2 is done”);

}, 3000);

});

process1

.then(output => {

console.log(output); // it display the Logs “Process 1 is done”

return process2; // it will Return another promise process2

})

.then(output => {

console.log(output); // it Logs “Process 2 is done”

})

.catch(error => {

console.log(“Display Error -“, error); // here it Catches any error in the process1 and process2 chain process

});

Explanation of Promise Chaining.

Here in this program process1 executes after 2 seconds, and the output is logged in the first .then() function.

Here the result of process1 is logged, and then process2 returns the value output, which is a new promise in the current program.

When process2 executes after 3 seconds, another .then() logs the result.

In this program, if any error is generated during the execution process of a promise, then the .catch() function method catches it.

Returning a promise from .then() in JavaScript.

In a JavaScript program, when the programmer returns a promise value from the .then() function callback method, it will wait for the returned promise to be resolved or rejected before forwarding to the next .then() function.

Example of returning a promise in JavaScript.

function process3() {

return new Promise((resolve, reject) => {

setTimeout(() => {

resolve(“Process 3 is done”);

}, 2000);

});

}

process1

.then(output => {

console.log(output); // it Logs the “process1 done”

return process3(); // it Return another promise process3

})

.then(output => {

console.log(output); // it Logs the “Process 3 is done”

})

.catch(error => {

console.log(“Display Error -“, error); // it Catch any error that occurs in chain process

});

In this program, process3() function is returned inside .then(), it means the second .then() function waits until process3() process is resolved.

Managing errors in promise chain in JavaScript.

Error management in promise chain becomes easy and effective with .catch() function in JavaScript programming. Where if any promise method in the chain process is rejected, then error management will be done in the .catch() function located at the end of the chain process. With this process, the programmer will not need to do separate program error management in each .then() function callback.

Basic error handling with .catch() in JavaScript.

const process4 = new Promise((resolve, reject) => {

setTimeout(() => {

reject(“process4 is done”);

}, 3000);

});

task1

.then(output => {

console.log(output); // it Logs the “process1 is done”

return process4; // it Return a promise that fails in chain method

})

.then(output => {

console.log(output); // here This don’t be executed because process4 fails in this process

})

.catch(error => {

console.log(“it Caught the error”, error); // it display the Logs the error when condition false

});

Explanation of errors in promise chain.

Here in this program process4 is rejected after 3 seconds.

Since here process4 process has failed, so .catch() function catches the program error and logs “it Caught the error” message.

Due to rejection in process4, the second .then() function is ignored.

Catching errors in any part of the chain in JavaScript.

In JavaScript programming, the .catch() function at the end of a promise chain in a method catches program errors generated in any previous .then() function block. This process is an important concept for central error management in an existing program, preventing programmers from adding a .catch() function to every .then() function.

Example of catching errors globally in JavaScript.

process1

.then(output => {

console.log(output); // it used to Logs “process 1 is done”

throw new Error(“it display error during process 2”);

})

.then(output => {

console.log(output); // here This don’t to be executed with the error

})

.catch(error => {

console.log(“it Caught error”, error.message); // it Catches the error part from any .then() function

});

Handling Multiple Promises with Promise.all() in JavaScript.

If in a JavaScript program the programmer is dealing with multiple promise conditions that may execute in parallel order, and the programmer wants to wait for all the promises to complete, then the programmer can apply the Promise.all() function in the current program. This process allows the programmer to execute multiple asynchronous tasks simultaneously and receive results when multiple program tasks complete.

Example of Promise.all() function.

const process5 = new Promise((resolve, reject) => {

setTimeout(() => {

resolve(“process 5 is done”);

}, 2000);

});

const process6 = new Promise((resolve, reject) => {

setTimeout(() => {

resolve(“process 6 is done”);

}, 3000);

});

Promise.all([process5, process6])

.then(output => {

console.log(output); // here it display the Logs [“process 5 is done”, “process 6 is done”]

})

.catch(error => {

console.log(“it display Error”, error); // here If any of the promises condition fail, it will log the error on screen

});

Explanation of Promise.all() function.

Here in the current program, the Promise.all() function waits for all the program promises to complete. If all the promises complete successfully, the .then() function block executes with an array of results displayed.

Here, if any promise in the array is rejected, the .catch() function block is automatically triggered in the program.

Here if any promise in the array fails in the program, then the .catch() function is triggered.

Handling multiple promises with Promise.race() in JavaScript.

In a JavaScript program, the Promise.all() function waits for all promises to complete, while the Promise.race() function will return immediately when the first promise in the program completes or is rejected. If the programmer needs a previously completed task, then this method can be important.

Example of Promise.race() function in JavaScript.

const proccess7 = new Promise((resolve, reject) => {

setTimeout(() => {

resolve(“process 7 is done”)

}, 2000);

});

const proccess8 = new Promise((resolve, reject) => {

setTimeout(() => {

resolve(“process 8 is done”);

}, 1000);

});

Promise.race([process7, proccess8])

.then(output => {

console.log(output); // here it Logs message on screen “process 8 is done” because it did it first

})

.catch(error => {

console.log(“it catch Error”, error); // here If any of the promises rejects it run catch

});

Explanation of Promise.race().

Here in this program, the first promise to be resolved by Promise.race() function is resolved with the result of proccess8 in this condition.

Even if proccess7 is resolved after proccess8, the race is resolved with the result of proccess8.

Summary of Chaining Promises in JavaScript.

  • Chaining Promises in JavaScriptJavaScript programmers can resolve multiple promises in series by applying the .then() function. Each .then() function returns a new promise, allowing you to create an order of asynchronous tasks.
  • Promises error management – The .catch() function is used to catch errors at any part of the chain in promises, creating centralized error management in program promises.
  • Promise.all() method – This function is applied when programmers want to wait for multiple program promises to resolve before proceeding.
  • Promise.race() method – This function is applied when programmers only think about the first promise to be resolved or rejected.

Leave a Reply