Till now, you have learned about the producer and the two consumers – then() and catch() methods. Have you noticed any similarity between the then() and the catch() method? Let’s look at that in the next video.
In the last video, you learned that when a promise is rejected (or there’s an error in the producer), the second argument of the then() method is also able to handle the error as well the argument of the catch() method is also able to handle the error. Thus, you got two ways to handle the error(s).
Following is the summary of the two ways of defining consumers:
WAY 1:
The then() method will be used to handle both - the success as well as the error(s).
.then(successCallback, errorCallback)
WAY 2:
The then() method will handle the success whereas the catch() method will handle the error(s).
.then(successCallback)
.catch(errorCallback)
Following is the example code, which you looked at:
WAY 1:
CASE A: When the promise is RESOLVED
// defining the promise object with the producer let promiseObj = new Promise((resolve, reject) => { console.log("Getting name from DB..."); setTimeout(() => { resolve("Srishti"); }, 3000); }); // defining the consumer - then() method promiseObj.then(val => { console.log(`Name received from DB = ${val}`); }, err => { console.log(`Error occurred = ${err}`); });
Output:
Getting name from DB...
Name received from DB = Srishti
CASE B: When the promise is REJECTED
// defining the promise object with the producer let promiseObj = new Promise((resolve, reject) => { console.log("Getting name from DB..."); setTimeout(() => { reject(new Error("Could not get the name from DB!")); }, 3000); }); // defining the consumer - then() method promiseObj.then(val => { console.log(`Name received from DB = ${val}`); }, err => { console.log(`Error occurred = ${err}`); });
Output:
Getting name from DB... Error occurred = Error: Could not get the name from DB!
WAY 2:
CASE A: When the promise is RESOLVED
// defining the promise object with the producer let promiseObj = new Promise((resolve, reject) => { console.log("Getting name from DB..."); setTimeout(() => { resolve("Srishti"); }, 3000); }); // defining the consumer - then() method promiseObj.then(val => { console.log(`Name received from DB = ${val}`); }); // defining the consumer - catch() method promiseObj.catch(err => { console.log(`Error occurred = ${err}`); });
Output:
Getting name from DB...
Name received from DB = Srishti
CASE B: When the promise is REJECTED
// defining the promise object with the producer let promiseObj = new Promise((resolve, reject) => { console.log("Getting name from DB..."); setTimeout(() => { reject(new Error("Could not get the name from DB!")); }, 3000); }); // defining the consumer - then() method promiseObj.then(val => { console.log(`Name received from DB = ${val}`); }); // defining the consumer - catch() method promiseObj.catch(err => { console.log(`Error occurred = ${err}`); });
Output:
Getting name from DB... Error occurred = Error: Could not get the name from DB! Uncaught (in promise) Error: Could not get the name from DB! at setTimeout
Till now, you learned that you should follow either of the two ways discussed above. You learned that you can have two arguments inside the then() method to handle success as well as error. Alternatively, you can have only one argument inside the then() method to handle only success & a catch() method to handle the error. But have you wondered what happens when you have two arguments inside the then() method (handling the success as well as the error) and a catch() method too to handle the error? Let’s check that out in the next video with Srishti.
In the last video, you learned that handling an error twice is undesirable. Thus, you should stick to using either of the ways discussed earlier to define the consumers.
Now is the time to attempt an exercise on promises based on everything you have learned till now. Watch the next video to view the exercise problem.
Hope that you are clear with the concept of promises by now. If you still have any confusion, it is highly recommended that you go through this concept again before moving on to the next segment.