Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 7996

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 60k
Author
Asked: November 28, 20242024-11-28T09:00:08+00:00 2024-11-28T09:00:08+00:00

How do you prevent promises swallowing errors

  • 60k

While using asynchronous code, JavaScript’s ES6 promises can make your life a lot easier without having callback pyramids and error handling on every second line. But Promises have some pitfalls and the biggest one is swallowing errors by default.
Let's say you expect to print an error to the console for all the below cases,

Promise.resolve("promised value").then(function () {   throw new Error("error"); });  Promise.reject("error value").catch(function () {   throw new Error("error"); });  new Promise(function (resolve, reject) {   throw new Error("error"); }); 
Enter fullscreen mode Exit fullscreen mode

But there are many modern JavaScript environments that won't print any errors. You can fix this problem in different ways,

1.Add catch block at the end of each chain
You can add catch block to the end of each of your promise chains.

Promise.resolve("promised value")   .then(function () {     throw new Error("error");   })   .catch(function (error) {     console.error(error.stack);   }); 
Enter fullscreen mode Exit fullscreen mode

But it is quite difficult to type for each promise chain and verbose too.

2.** Add done method**
You can replace first solution's then and catch blocks with done method.

Promise.resolve("promised value").done(function () {   throw new Error("error"); }); 
Enter fullscreen mode Exit fullscreen mode

Let's say you want to fetch data using HTTP and later perform processing on the resulting data asynchronously. You can write done block as below,

getDataFromHttp()   .then(function (result) {     return processDataAsync(result);   })   .done(function (processed) {     displayData(processed);   }); 
Enter fullscreen mode Exit fullscreen mode

In future, if the processing library API changed to synchronous then you can remove done block as below,

getDataFromHttp().then(function (result) {   return displayData(processDataAsync(result)); }); 
Enter fullscreen mode Exit fullscreen mode

and then you forgot to add done block to then block leads to silent errors.

3.Extend ES6 Promises by Bluebird
Bluebird extends the ES6 Promises API to avoid the issue in the second solution. This library has a “default” onRejection handler which will print all errors from rejected Promises to stderr. After installation, you can process unhandled rejections.

Promise.onPossiblyUnhandledRejection(function (error) {   throw error; }); 
Enter fullscreen mode Exit fullscreen mode

and discard a rejection, just handle it with an empty catch.

Promise.reject("error value").catch(function () {}); 
Enter fullscreen mode Exit fullscreen mode

Stop JavaScript Promises Swallowing Exceptions.

It’s very hard to debug a crash when no stack traces are printed. It becomes a case of manually trying to find the error.

GET /foo/bar/ Doing something useful Error: Expected } near ; 
Enter fullscreen mode Exit fullscreen mode

ES6 promises doesn’t seem to offer the functionality to change this, and bluebird has on[Possibly]UnhandledRejection, which can only be used if you don’t add a .catch() case to the promise. There is no global callback for a rejection unless it’s unhandled. To workaround this, we’re going to need to override the method which runs the callbacks. This is a little hacky, and relies on the library not changing – but it’s better than swallowing errors.

First, if you haven’t already, install Bluebird.

npm install --save bluebird 
Enter fullscreen mode Exit fullscreen mode

Next, make a file somewhere (perhaps called bluebird.js) with this as its contents.

const Promise = require('bluebird')  // Throw errors in promises rather than calling reject() // Makes debugging A LOT easier Promise.prototype._rejectCallback_old = Promise.prototype._rejectCallback Promise.prototype._rejectCallback =     function(reason, synchronous, ignoreNonErrorWarnings) {         if (reason.stack) {             throw reasong         } else {             this._rejectCallback_old(reason, synchronous, ignoreNonErrorWarnings)         }     }  module.exports = Promise 
Enter fullscreen mode Exit fullscreen mode

Alternatively you could just print reason.stack if it exists, however I prefer a full crash whilst debugging.

beginnersjavascriptnodewebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.