-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add .catch to mpromise #2308
Comments
+1 |
2 similar comments
👍 |
+1 |
Is monogoose.js even being maintained anymore, given that such an important part of the promise spec is missing and hasn't landed yet? |
Obviously it is maintained and developed: https://github.com/LearnBoost/mongoose/commits/master |
Sure, this should be reasonably easy to add. Unfortunately |
I'm going by the ES6 spec, where The issue is actually I was able to monkey patch mongoose using the following code (and note, that // patch mongoose to include .catch
mongoose.Query.prototype.exec = function exec(op, callback) {
if ('function' === typeof op) {
callback = op;
op = null;
} else if ('string' === typeof op) {
this.op = op;
}
var promise = new Promise(function (resolve) {
if (!this.op) {
return resolve();
}
mongoose.Query.base.exec.call(this, op, function (error, document) {
if (error) {
throw error;
}
resolve(document);
});
}.bind(this));
if (callback) {
promise.then(function (result) {
callback(null, result);
}).catch(function (error) {
callback(error, null);
});
}
return promise;
}; I think my concern is that I expect in some unknown future to be able to remove all promise libraries in favour of native promises, and since native promises in V8 will include I'd propose moving away from It's not a big change to the library (just promise.js in mongoose) but I'm not 100% sure of the impact on tests. I did also notice weird behaviour in my code after I changed the internals of |
Is there a good guide out there to all the functionality included in V8 native promises? I'd like to look into it, but I'm somewhat hesitant to base mongoose's promise API on V8 because V8 famously breaks its API pretty regularly. |
@vkarpov15 the link I used in my comment earlier is an export of the actual spec (just as HTML). I've been using @jakearchibald's article as a pretty good guide since it boils down the API: http://www.html5rocks.com/en/tutorials/es6/promises/#toc-api |
Bluebird's reporting uncatched rejections is actually a powerful and useful feature (coupled with long stack traces). We use this feature to fail tests that don't have proper error handling. You can just as easily disable it:
|
👍 |
6 similar comments
+1 |
+1 |
+1 |
+1 |
+1 |
+1 |
👍 Lack of catch in mongoose is a pain. Have to wrap every call with native Promises. |
See #2688 |
+1 for supporting native ES6 Promises |
+1 |
Probably already mentioned elsewhere, but I just use bluebird's let Promise = require("bluebird")
let mongoose = Promise.promisifyAll(require('mongoose')) coupled with export let userQueries = {
// lots of other queries
findBy: {
email: (email) => User.findOne({ email }).execAsync()
}
} Then, in function login (req, email, password, done) {
userQueries.findBy.email(email)
/* lots of `then`s */
}).catch((err) => {
return done(null, false, req.flash('message', err.message || err))
})
} |
+1 |
1 similar comment
+1 |
+1. This is a pretty important feature, and for everyone jumping on ES6, this completely breaks immersion. I have code all over the place using @royhowie @remy @assaf Is there a possible hit to performance when wrapping mongoose with Bluebird's |
See #2688, 4.1.0 supports the below
or whichever ES6 promise constructor you prefer. |
@vkarpov15 A-ha! Thanks a bunch. This is awesome. |
Hi @vkarpov15 just tried it with 4.1.0 and it doesn't seem to work for me. Throws an error that says undefined is not a function when I use |
@zekenie Works for me, actually. Here's how I do it now on 4.1.0 -
Can you show us some code to help debug your problem? |
@zekenie are you actually setting |
I think for the next major version, maybe switch to native promise implementation by default? |
@adambuczynski yep we won't support mpromise at all in 5.0, will use native promises by default but support switching to whichever ES2015 promise constructor lib you want: https://github.com/Automattic/mongoose/wiki/5.0-Deprecation-Warnings |
👍 |
@vkarpov15 without any hard stats, my gut says more and more people are using Bluebird. Strongloop is converting everything to use Bluebird in 3.0 of loopback. Do you think we can emphasize tests cases and code coverage for promises with bluebird in v5 even though v5 will technically be only supporting ES2015 promises? |
@Jeff-Lewis yeah I see a lot of people going to bluebird as well. On the other hand, with ideas like co and async/await, I haven't cared which promise library I'm using since mid-2015, and minimal promise libs like https://www.npmjs.com/package/es6-promise are seeing good adoption too. v5 will support bluebird in the same way that >= 4.1 supports bluebird. There's really not much more we can do short of switching over to bluebird by default, and bluebird's too quirky and bloated for my tastes. |
@vkarpov15 what quirks do you think it has? I was under the impression that it was quite a decent promise library, after having a look at it last week (I was using native promises and Angular's $q before that), and I was considering using it for both backend and frontend. But one big thing missing from native promises for example, is that there is no Also, Bluebird didn't seem too bloated, and even if it were, the performance gains over other libraries were quite decent. |
Honestly I've never been a big fan of My beef with bluebird is:
Contrast this with http://npmjs.org/package/es6-promise - nice neat minimal implementation that you can grok in 15 minutes https://github.com/jakearchibald/es6-promise/blob/master/lib/es6-promise/promise.js . |
Fair points, thanks for your feedback. While you can pass in a second function to But I agree, in Node apps, there is much less need for a |
@vkarpov15 Regarding 3) Weird bugs: mongoosejs/mquery#66, I added a comment to it. |
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 ( #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.
The text was updated successfully, but these errors were encountered: