/
recurse.js
40 lines (36 loc) · 1.15 KB
/
recurse.js
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
{
module.exports = function IterateOver(dice, iterator, callback){
// this controls the amount of dice pages we pull from
loops = 3;
var superArray = [];
var doneCount = 0;
var options = dice;
function report(data) {
superArray = superArray.concat(data.resultItemList);
doneCount++;
if (doneCount === loops) {
callback(superArray);
}
}
for (var i = 1; i < loops + 1; i++) {
var improv = Object.assign({}, dice);
improv.path += ('&page=' + i);
iterator(improv, report);
improv.path = options.path;
}
}
}
//r takes options and a callback
//the callback gets passed the object response from DICE.
//call IterateOver
//set dice to options var
// call iterator/r passing it options and a callback, report.
//request searches with the options
//iterator/r calls callback/report with Dice data
//report concats DICEdata onto superArray
//report increments doneCount by 1
//report checks if loop var = doneCount
//does not, so loops again.
//if loop var === doneCount
//call callback/function(data) { res.json({data, body})}
//callback gets called, exporting finished superArray