Add .catch to mpromise
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
Implement .catch Method for mpromise
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
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.
javascriptmpromise.prototype.catch = function(onRejected) { return this.then(null, onRejected); }; - 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
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.
javascriptdescribe('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
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
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
Alex Chen
2450 rep