Skip to content

Commit 3366694

Browse files
authored
Merge pull request #85 from lesterzone/internal-promises
Simple way to handle/create required promises internally
2 parents d8e8299 + 8581975 commit 3366694

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

index.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ function paginate(query, options, callback) {
2828
let leanWithId = options.hasOwnProperty('leanWithId') ? options.leanWithId : true;
2929
let limit = options.hasOwnProperty('limit') ? options.limit : 10;
3030
let page, offset, skip, docsQuery, promises;
31+
3132
if (options.offset) {
3233
offset = options.offset;
3334
skip = offset;
@@ -39,28 +40,25 @@ function paginate(query, options, callback) {
3940
offset = 0;
4041
skip = offset;
4142
}
43+
4244
if (limit > 0) {
4345
docsQuery = this.find(query)
4446
.select(select)
4547
.sort(sort)
4648
.skip(skip)
4749
.limit(limit)
4850
.lean(lean);
51+
4952
if (populate) {
50-
[].concat(populate).forEach((item) => {
51-
docsQuery.populate(item);
52-
});
53+
[].concat(populate).forEach((item) => docsQuery.populate(item));
5354
}
5455
}
55-
promises = {
56-
docs: (docsQuery) ? docsQuery.exec() : false,
57-
count: this.count(query).exec()
58-
};
59-
promises = Object.keys(promises).map((index) => {
60-
if (promises[index]) {
61-
return promises[index];
62-
}
63-
});
56+
57+
promises = [
58+
docsQuery ? docsQuery.exec() : Promise.resolve({}),
59+
this.count(query).exec()
60+
];
61+
6462
let promise = new Promise((resolve, reject) => {
6563
Promise.all(promises).then((data) => {
6664
let docs = (limit > 0) ? data[0] : [];
@@ -74,7 +72,7 @@ function paginate(query, options, callback) {
7472
result.docs = result.docs.map((doc) => {
7573
doc.id = String(doc._id);
7674
return doc;
77-
});
75+
});
7876
}
7977
if (offset !== undefined) {
8078
result.offset = offset;
@@ -94,6 +92,7 @@ function paginate(query, options, callback) {
9492
reject(error);
9593
});
9694
});
95+
9796
return promise;
9897
}
9998

0 commit comments

Comments
 (0)