Promises and Error Handling

What is promise?

Promise provide us an alternative approach of asynchronous operation which is clean, more readable and flexible when compared to traditional callback-based approaches. It is very useful when we chain multiple async operation and after that we do somethings.  Also promise allow us to handle asynchronous error using approaches that are similar to synchronous try/catch

If we search in google then we will get enough definition and example of promises. And I am pretty sure that we will understand and we will be ready to use promise in our project.

But in this post i will try to mention some important things. So that we can think when we are going to use promise.

Story 1: Suppose if we add a catch handler at the end of our each promise method what will be happened?

Lets see with an example:

let getData = function () {
  return User.findOne().exec().then(function (userData) {
    // add some logic here
    return userData;
  }).catch(function (err) {
    console.log('error from getData', err);
  });
};

It is very simple code- here we are going populate user first record , after that we will add some logic and return the updated user record.

Now we are going to call the getData method. see the example code

getData().then(function (userData) {
  // add some logic here
  console.log('Success');
  return userData;
}).catch(function (err) {
  console.log('error from Success', err);
});

So, we don’t have any problem if we run the above code. Because we don’t  add any logic and we assume that we don’t have any error from get user from User Model (User.findOne()) . So, all looks good.

But if we get throw an exception in the method getData . For example, see the code bellow:

let getData = function () {
  return User.findOne().exec().then(function (userData) {
    // add some logic here
    throw new Error('getData logic error. ');
    return userData;
  }).catch(function (err) {
    console.log('error from getData');
  });
};

Again when we run the code using:

getData().then(function (userData) {
  // add some logic here
  console.log('Success');
  return userData;
}).catch(function (err) {
  console.log('error from Success');
});

We will see the following output:

error from getData
Success

Even if it is threw exception – though it shows success. That will be not expected.

Later I will try write some different stories so that we can think when we will use Promises.

 

You May Also Like

About the Author: Md. Delwar Hossain

He has 11 years of experience in developing standalone software and web applications for multiple database platforms. He has been passionate about new tools and technologies. He is positive and trustworthy. He is capable to learn and adapt quickly to different situations. He is a great team player and enjoys leading and mentoring. He is specialized in architecting and building complex web and mobile application. He has strong skills to automate POS, inventory, supply chain, trading export/ import, human resource management, manufacturing and production, distribution management system and hospital management system.

6 Comments

  1. We are a bunch of volunteers and starting a new scheme in our community. Your site provided us with valuable information to work on. You’ve done a formidable task and our whole community might be thankful to you.

  2. I’ve been having issues with my Windows hosting. It has set me back quite a bit while making the next list. This is the current list that I have. I should add another list in less than a week. I’ll let you all know when the next list is ready. Thank you for your patience.

Leave a Reply

Your email address will not be published. Required fields are marked *