-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparseFindAll.js
More file actions
73 lines (59 loc) · 1.81 KB
/
Copy pathparseFindAll.js
File metadata and controls
73 lines (59 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
(function(root, factory){
// Node env
if (typeof module !== 'undefined' && module.exports) {
module.exports = factory(require('cloud/async.js'),root);
}
// Browser env
else {
root.parseFindAll = factory(root.async, root);
}
}(this,function(async,root){
var findAll = function(query,callbackHash) {
var promise = new Parse.Promise;
var succCbDefined = typeof callbackHash !== "undefined" && typeof callbackHash.success !== "undefined";
var errCbDefined = typeof callbackHash !== "undefined" && typeof callbackHash.error !== "undefined";
query.count({
success:function(count) {
// Get the number of iterations (queries) we'll have to make.
var nbIterations = Math.ceil(count / 1000);
// Build the "limit array"
var skipLimitArray = [];
// Construct our skipLimitArray so we can chain it with async lib
for (var i = 0; i < nbIterations; i++) {
skipLimitArray.push(i*1000);
}
// allData will contain all the concatened results
var allData = [];
// Let's loop on it -- with async flow control indeed
async.forEachSeries(skipLimitArray,function(currentSkip,callback){
query.limit(1000).skip(currentSkip);
query.find({
success:function(results) {
allData = allData.concat(results);
callback();
},
error:function(err) {
callback(err);
}
});
},
function(err) {
// Any error during queries? Let's pass it back
if (err) {
if (errCbDefined) callbackHash.error(err);
promise.reject(err);
}
if (succCbDefined) callbackHash.success(allData);
promise.resolve(allData);
});
},
error:function(err) {
if (errCbDefined) callbackHash.error(err);
promise.reject(err);
}
});
// Make it promise compatible -- dope
return promise;
};
return findAll;
}));