I've the following mongoose queries which each producing the same result but with different Promise implementations. I've tested the scenario where mongooseModel throws an error and again each implementation produces the same result i.e. the calling method catches the exception and returns the correct error message.
What I'm wondering is which is the best approach? The fnCall is more readable and makes more sense when reading! In my opinion anyway. Whats important here is a repeatable pattern that we can use across all our NodeJS modules.
First way:
ListService.prototype.getItems = function (queryParams, restrictFields, startIndex, pageSize) {
return Promise.fnCall(function() {
return mongooseModel().find(queryParams, restrictFields)
.skip(startIndex)
.limit(pageSize)
.lean(true)
.exec();
})
.then(function (items) { return items; })
.catch(function (error) {return error; });
};
Second way:
ListService.prototype.getItems = function (queryParams, restrictFields, startIndex, pageSize) {
return new Promise(function (resolve, reject) {
mongooseModel().find(queryParams, restrictFields)
.skip(startIndex)
.limit(pageSize)
.lean(true)
.exec(function (err, items) {
if (err) {
reject(err);
}
else {
resolve(items);
}
});
};
Aucun commentaire:
Enregistrer un commentaire