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.

134 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.

  3. Pingback: içgiyimuw
  4. Pingback: backlink list
  5. Pingback: Branding
  6. hi!,I love your writing very so much! percentage we keep up a correspondence more about your post on AOL? I require an expert in this area to unravel my problem. Maybe that is you! Having a look forward to look you.

  7. hello!,I really like your writing so so much! percentage we keep up a correspondence extra about your article on AOL? I require an expert on this area to resolve my problem. Maybe that is you! Taking a look forward to look you.

  8. Hiya, I’m really glad I have found this info. Today bloggers publish just about gossip and net stuff and this is really annoying. A good website with interesting content, that is what I need. Thank you for making this web-site, and I will be visiting again. Do you do newsletters by email?

  9. Ankara küçük nakliyat firması olarak, Ankara’nın her semtinde küçük kamyonetlerimizle orta boy araçlarımızla hizmet vermekteyiz. Ankara küçük nakliyat firması olarak büyük kamyonetlerle girilmesi zor olan sokaklara küçük araçlarımızla hizmet vermekteyiz. Ankara küçük nakliyat firması olarak güler yüzlü ekibimizle ve bakımlı içi dışı tertemiz araçlarımızla siz Ankara halkına hizmet vermekteyiz. Ankara küçük nakliyat firması bulmak kolaydır ama kaliteli güvenli uygun fiyat garantisiyle çalışan firma sayısı azdır. https://ankarakucuknakliyat.gen.tr/ Kaliteli güvenli uygun fiyat garantisi veren firmalar arasında Ankara küçük nakliyat firması olarak şüphesiz bizi tercih edebilirsiniz.

  10. Hi there. I discovered your blog via Google while looking for a comparable matter, your site came up. It seems great. I have bookmarked it in my google bookmarks to visit then.

  11. Hiya, I’m really glad I’ve found this information. Nowadays bloggers publish only about gossip and web stuff and this is really annoying. A good blog with exciting content, this is what I need. Thanks for making this web site, and I’ll be visiting again. Do you do newsletters by email?

  12. Hiya, I’m really glad I’ve found this info. Today bloggers publish only about gossip and net stuff and this is really frustrating. A good website with exciting content, that’s what I need. Thank you for making this web-site, and I will be visiting again. Do you do newsletters by email?

  13. Hi there. I discovered your website via Google even as looking for a related subject, your website came up. It seems to be good. I have bookmarked it in my google bookmarks to visit then.

  14. Hiya, I’m really glad I’ve found this information. Nowadays bloggers publish only about gossip and web stuff and this is really annoying. A good website with interesting content, that is what I need. Thanks for making this web site, and I’ll be visiting again. Do you do newsletters by email?

  15. Hiya, I’m really glad I’ve found this info. Today bloggers publish just about gossip and web stuff and this is really frustrating. A good blog with interesting content, this is what I need. Thank you for making this web site, and I’ll be visiting again. Do you do newsletters by email?

  16. Wonderful goods from you, man. I have understand your stuff prior to and you are just too excellent. I really like what you’ve bought right here, really like what you’re saying and the way in which through which you assert it. You’re making it entertaining and you still take care of to stay it smart. I can not wait to learn far more from you. That is really a great website.|

  17. I’m so happy to read this. This is the kind of manual that needs to be given and not the random misinformation that is at the other blogs. Appreciate your sharing this best doc.

  18. Hiya, I’m really glad I’ve found this information. Nowadays bloggers publish just about gossip and internet stuff and this is really frustrating. A good site with interesting content, that’s what I need. Thanks for making this site, and I’ll be visiting again. Do you do newsletters by email?

  19. Hiya, I am really glad I’ve found this information. Nowadays bloggers publish just about gossip and net stuff and this is really frustrating. A good website with interesting content, that is what I need. Thanks for making this web site, and I’ll be visiting again. Do you do newsletters by email?

  20. Kayseri evden eve nakliyat sektöründe görev yapmakta olan nakliye firmaları arasından en iyilerini sitemizdeki siz değerli müşterilerimize kaliteli ve profesyonel hizmet sunması için bir araya getirdik. Kayseri başta olmak üzere merkez ve çevre ilçelerine evden eve taşımacılık yapma kapasitesine sahip bu kuruluşlarımız aynı zamanda diğer şehirlere de eşyalarınızı taşıyabiliyorlar. Kayseri evden eve nakliyat firması arayışı içinde kendinizi buldunuz ve ne yapacağınızı bilmiyorsanız sitemiz tam size göre hizmet sunanların buluşma platformu. En uygun fiyatlar, gerçek müşteri yorumları sonucunda kaliteden ödün vermeden çalışan onlarca firma sizlere hizmet etmeye hazır.

  21. Unquestionably believe that which you said. Your favorite justification seemed to be on the net the easiest thing to be aware of. I say to you, I certainly get irked while people consider worries that they just do not know about. You managed to hit the nail upon the top as well as defined out the whole thing without having side effect , people can take a signal. Will likely be back to get more. Thanks

  22. Normally I do not learn article on blogs, but I wish to say that this write-up very pressured me to try and do it! Your writing style has been surprised me. Thanks, quite nice post.

  23. Hi Bro, hairstyles picture Thank you very much for your writing and your article and I wish you continued success. Please try to produce such valuable content in the following processes. In addition to this article, I am very happy to see your unique content.

  24. Hiya, I’m really glad I have found this information. Today bloggers publish just about gossip and web stuff and this is actually frustrating. A good web site with exciting content, that’s what I need. Thank you for making this web site, and I’ll be visiting again. Do you do newsletters by email?

  25. Hiya, I’m really glad I’ve found this info. Nowadays bloggers publish just about gossip and internet stuff and this is really frustrating. A good blog with interesting content, this is what I need. Thank you for making this site, and I’ll be visiting again. Do you do newsletters by email?

  26. I do trust all of the ideas you have presented in your post. They’re really convincing and can certainly work. Still, the posts are very quick for beginners. Could you please lengthen them a bit from subsequent time? Thank you for the post.

  27. Hello, I think your website might be having browser compatibility issues.
    When I look at your blog in Safari, it looks fine but when opening in Internet Explorer, it has
    some overlapping. I just wanted to give you a quick heads up!

    Other then that, awesome blog!

  28. Hi, i think that i saw you visited my site thus i came to “return the favor”.I am trying to find things to enhance my web site!I suppose its ok to use a few of your ideas!!

  29. magnificent post, very informative. I wonder why the other specialists of this sector do not notice this. You should continue your writing. I’m sure, you’ve a huge readers’ base already!

  30. I think everything posted made a ton of sense. But, what about this? suppose you were to create a awesome headline? I mean, I don’t wish to tell you how to run your website, but suppose you added a headline that makes people want more? I mean BLOG_TITLE is kinda boring. You could peek at Yahoo’s home page and see how they create news headlines to grab viewers to open the links. You might add a related video or a pic or two to grab people interested about what you’ve written. Just my opinion, it would bring your website a little bit more interesting.|

  31. I would like to thnkx for the efforts you have put in writing this web site. I’m hoping the same high-grade blog post from you in the upcoming as well. Actually your creative writing skills has inspired me to get my own site now. Actually the blogging is spreading its wings quickly. Your write up is a good example of it.

  32. Bundan birkaç yıl önce Temmuz 2015’te kültür / sanat dünyasındaki haberleri, müzik ve sinemaya dair konuları inceleyen, konser / festival ya da sanatın her alanıyla ilgili etkinliklere gidip hem fotoğraf hem de yazıyla sunabilen, röportajlar kısmında alanında iyi olanlarla sohbet eden, keşif alanında yeni bir dünyaya adım attırıcı zenginlikte içerik üretmeye çalışıyoruz.

    Bu içeriklerin dışında sosyal medyadaki birçok sosyal ağı etkin kullanıp, ziyaretçileriyle etkileşim halinde olan Sertsesli paylaşımlarına samimi yaklaşıyor.

    Haberler

    Kültür / Sanat dünyasından en son haberleri samimi bir dille hızlı bir şekilde sunuyoruz.

    Teknoloji

    Teknoloji alanındaki gelişmeleri takip edip, tecrübemiz ile birleştirip paylaştığımız atölye.

  33. The root of your writing while sounding agreeable at first, did not settle properly with me personally after some time. Somewhere within the sentences you actually were able to make me a believer but only for a short while. I however have got a problem with your leaps in logic and one would do well to help fill in those gaps. When you can accomplish that, I will certainly be impressed.

  34. It¡¦s actually a great and useful piece of information. I am glad that you shared this helpful information with us. Please stay us informed like this. Thanks for sharing.

  35. I loved as much as you’ll receive carried out right here. The sketch is tasteful, your authored material stylish. nonetheless, you command get bought an shakiness over that you wish be delivering the following. unwell unquestionably come further formerly again since exactly the same nearly a lot often inside case you shield this increase.

  36. Hi there. I discovered your website by means of Google whilst searching for a similar topic, your site got here up. It appears to be good. I’ve bookmarked it in my google bookmarks to come back then.

  37. Unquestionably believe that which you said. Your favorite reason seemed to be on the internet the simplest thing to be aware of. I say to you, I definitely get annoyed while people think about worries that they plainly do not know about. You managed to hit the nail upon the top as well as defined out the whole thing without having side effect , people can take a signal. Will likely be back to get more. Thanks

  38. Hiya, I am really glad I have found this info. Today bloggers publish only about gossip and web stuff and this is really annoying. A good web site with interesting content, that is what I need. Thank you for making this web site, and I will be visiting again. Do you do newsletters by email?

  39. Hiya, I’m really glad I have found this information. Today bloggers publish just about gossip and web stuff and this is actually irritating. A good site with exciting content, that is what I need. Thanks for making this site, and I will be visiting again. Do you do newsletters by email?

  40. Hiya, I’m really glad I have found this info. Nowadays bloggers publish just about gossip and internet stuff and this is really frustrating. A good site with interesting content, this is what I need. Thanks for making this web-site, and I’ll be visiting again. Do you do newsletters by email?

  41. Hiya, I’m really glad I have found this information. Today bloggers publish just about gossip and net stuff and this is really irritating. A good website with exciting content, this is what I need. Thank you for making this site, and I’ll be visiting again. Do you do newsletters by email?

  42. Having read this I believed it was very informative. I appreciate you taking the time and effort to put this article together. I once again find myself personally spending a lot of time both reading and commenting. But so what, it was still worth it!|

  43. My spouse and I absolutely love your blog and find a lot of your post’s to be exactly I’m looking for. Would you offer guest writers to write content to suit your needs? I wouldn’t mind publishing a post or elaborating on most of the subjects you write concerning here. Again, awesome web site!

  44. Ankara şehirlerarası nakliyat işlemlerinde güzel Ankara’mızın en çok tercih edilen lider firmasıyız. Profesyonel kadrolarımız, son model nakliye araç ve modüler yük asansörlerimiz yine aynı şekilde kaliteli paketleme malzemelerimiz sayesinde sorunları ortadan kaldırarak memnuniyet dolu bir sürece imza atıyoruz. Ankara şehirlerarası nakliyat firmamız ülke geneli müşterilerin taleplerini yerine getirme kapasitesine sahip kurumsal ve ilkeli bir kuruluştur. En iyi taşımacılık alt yapısına sahip olmamız sayesinde hem müşterilerimize hem sektörümüze oldukça büyük katkılar sağlamaktayız.

  45. Hi Bro, Thank you very much for your writing and your article and I wish you continued success. Please try to produce such valuable content in the following processes. In addition to this article, I am very happy to see your unique content.

  46. Hey! I know this is kind of off-topic but I needed to ask. Does managing a well-established blog such as yours require a large amount of work? I am brand new to running a blog but I do write in my journal every day. I’d like to start a blog so I can easily share my experience and feelings online. Please let me know if you have any suggestions or tips for brand new aspiring bloggers. Thankyou!

  47. Hi there, i read your blog from time to time and i own a similar one and i was just curious if you get a lot of spam responses? If so how do you protect against it, any plugin or anything you can recommend? I get so much lately it’s driving me insane so any assistance is very much appreciated.

  48. It’s a pity you don’t have a donate button! I’d definitely donate to this fantastic blog! I guess for now i’ll settle for book-marking and adding your RSS feed to my Google account. I look forward to new updates and will share this website with my Facebook group. Chat soon!

  49. Hey! Quick question that’s totally off topic. Do you know how to make your site mobile friendly? My website looks weird when browsing from my apple iphone. I’m trying to find a template or plugin that might be able to resolve this problem. If you have any recommendations, please share. Cheers!

  50. Have you ever considered creating an ebook or guest authoring on other websites? I have a blog based on the same subjects you discuss and would really like to have you share some stories/information. I know my readers would value your work. If you are even remotely interested, feel free to shoot me an e mail.

  51. Hiya, I’m really glad I’ve found this information. Today bloggers publish just about gossip and web stuff and this is really frustrating. A good web site with exciting content, that’s what I need. Thanks for making this web-site, and I’ll be visiting again. Do you do newsletters by email?

  52. Hiya, I am really glad I have found this information. Today bloggers publish just about gossip and internet stuff and this is actually irritating. A good website with exciting content, that’s what I need. Thanks for making this site, and I will be visiting again. Do you do newsletters by email?

  53. Hiya, I am really glad I have found this info. Today bloggers publish only about gossip and net stuff and this is really annoying. A good web site with interesting content, that’s what I need. Thanks for making this web-site, and I will be visiting again. Do you do newsletters by email?

  54. Hiya, I am really glad I have found this info. Nowadays bloggers publish just about gossip and net stuff and this is actually frustrating. A good blog with interesting content, that’s what I need. Thank you for making this web-site, and I will be visiting again. Do you do newsletters by email?

  55. Hello, Neat post. There is an issue together with your site in internet explorer, may check this¡K IE nonetheless is the market chief and a large component to other people will omit your magnificent writing due to this problem.

  56. I am usually to running a blog and i actually recognize your content. The article has really peaks my interest. I am going to bookmark your site and keep checking for new information.

  57. Excellent goods from you, man. I’ve take into accout your stuff prior to and you are simply too excellent. I really like what you have acquired here, certainly like what you’re stating and the way in which in which you are saying it. You make it enjoyable and you still care for to stay it sensible. I can not wait to read far more from you. That is actually a great site.

  58. Excellent goods from you, man. I’ve understand your stuff previous to and you are just extremely wonderful. I really like what you’ve acquired right here, certainly like what you are saying and the best way by which you are saying it. You make it entertaining and you still care for to keep it sensible. I can’t wait to read far more from you. This is really a tremendous website.

Leave a Reply to http://gourmet-cabby.com Cancel reply

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