I have read the two discussions (#2571 and #517) that relate to this opportunity, and still do not see any reason to abandon it. Why not? Even if Mongoose should maintain compatibility with the ES5, this does not prevent to check that the setter returns to us. Watch this pseudo-code:
if (setterResult.constructor.name === 'Promise' && setterResult.then) {
promisesArray.push(setterResult.then(
value => {
return {
path: path,
value: value
};
},
err => {
return err;
}
));
}
// ...
if (promisesArray.length){
return Promise.all(promisesArray).then(
results => {
for (let result of results) {
// Do anything with it
}
// Do anything with all fields
return this.save();
},
errors => {
for (let err of errors) {
// Do anything with it
}
return errors;
}
);
} else {
// Do anything with all fields
return this.save();
}
So why not? This is a very useful feature, and it will join naturally in existing.
Yeah async setters would be useful. It's more difficult than you think, because the setter would need to resolve before doing validate()
, and you'd need to return a promise from .set()
because .set()
can throw an error if there's a cast error.
Would love to see that coming in Mongoose !
Do you plan to implement that in a next release @vkarpov15 ?
@maxs15 possibly. Still haven't investigated how we would implement this feature, so uncertain.
I've written a little plugin to mock async getters/setters until it lands in the core. It uses different properties (read
instead of get
and write
instead of set
) to simplify things and middleware (pre init
and post save
for getters and pre save
for setters). It is by no means perfect (I'm especially not happy how it interacts with validators) but it gets the job done.
I like this plugin but there's a few caveats, we'd also have to integrate this queries, because we also support doing things like findByIdAndUpdate(doc, doc)
I would be happy to see that feature.
What's the status?
+1
May be we ready for PR? Oh I wait this so long. =)
Hi what is status on this?
Library worked great with mongoose 4.
@Matzu89 what library?
Most helpful comment
I would be happy to see that feature.
What's the status?