FG
๐Ÿ’ป Software๐Ÿ—„๏ธ DatabasesMongoDB

Add .catch to mpromise

Fresh3 days ago
Mar 14, 20260 views
Confidence Score55%
55%

Problem

Hey, I noticed that mpromise does not have a .catch, like the promise library we use (bluebird). It's a nice and simple feature, and makes one's promise chains a quite readable. Note, that I did check, that .catch is not part of the Promise/A+ specs, however it does seem to be part of the ECMA Script 6 proposed specs. ( https://people.mozilla.org/~jorendorff/es6-draft.html#sec-promisecatch ) I saw the discussion here ( https://github.com/LearnBoost/mongoose/issues/1699 ), that mpromise will eventually be just a schim until node promises arrive, and I checked out unstable node, the Promise there did have a .catch.

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix โ€“ Awaiting Verification

Implement .catch Method for mpromise

Medium Risk

The mpromise library currently lacks a .catch method, which is a common feature in modern promise implementations, such as those in ES6 and libraries like Bluebird. This absence makes error handling in promise chains less readable and can lead to unhandled promise rejections.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Modify mpromise to include .catch

    Extend the mpromise prototype to include a .catch method that functions similarly to the .catch method in native promises. This method will accept a rejection handler and return a new promise.

    javascript
    mpromise.prototype.catch = function(onRejected) {
      return this.then(null, onRejected);
    };
  2. 2

    Update Documentation

    Ensure that the documentation for mpromise is updated to reflect the new .catch method, including usage examples to assist developers in adopting this feature.

  3. 3

    Run Unit Tests

    Create and run unit tests to verify that the .catch method behaves as expected, including scenarios for handling rejections and chaining with other promise methods.

    javascript
    describe('mpromise .catch', function() {
      it('should handle rejections', function(done) {
        const promise = new mpromise();
        promise.catch(function(err) {
          expect(err).to.exist;
          done();
        });
        promise.reject(new Error('Test Error'));
      });
    });
  4. 4

    Test Integration with Existing Code

    Review existing codebases that utilize mpromise to ensure that the new .catch method integrates seamlessly without introducing breaking changes.

  5. 5

    Publish Updated Version

    Once testing is complete and all issues are resolved, publish the updated version of mpromise with the new .catch method included.

Validation

To confirm the fix worked, write a simple promise chain using mpromise that includes a .catch method. Ensure that it correctly handles rejections and that the output matches expected results. Additionally, run all unit tests to verify no existing functionality is broken.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

mongoosemongodbodm