I have something like
new Promise (resolve, reject) ->
trader.getTrades limit, skip, (err, trades) ->
return reject err if err
resolve trade
Here's my own solution to paging through promises: method page, as part of the spex library.
It also lets you throttle the processing and provide load balancing as needed.
var promise = require('bluebird');
var spex = require('spex')(promise);
function source(index, data, delay) {
// create and return an array/page of mixed values
// dynamically, based on the index of the sequence;
switch (index) {
case 0:
return [0, 1, promise.resolve(2)];
case 1:
return [3, 4, promise.resolve(5)];
case 2:
return [6, 7, promise.resolve(8)];
}
// returning nothing/undefined indicates the end of the sequence;
// throwing an error will result in a reject;
}
function dest(idx, data, delay) {
// data - resolved array data;
console.log("LOG:", idx, data, delay);
}
spex.page(source, dest)
.then(function (data) {
console.log("DATA:", data); // print result;
});
LOG: 0 [ 0, 1, 2 ] undefined
LOG: 1 [ 3, 4, 5 ] 3
LOG: 2 [ 6, 7, 8 ] 0
DATA: { pages: 3, total: 9, duration: 7 }