You should be able to use promisify()
/promisifyAll()
to convert trader.getTrades()
to an async version that returns a promise. Then, something like this should work well:
function getAllTrades(limit, offset, query) {
var allTrades = [];
function getTrades(limit, offset, query){
return trader.getTradesAsync(limit, offset, query)
.each(function(trade) {
allTrades.push(trade)
// or, doStuff(trade), etc.
})
.then(function(trades) {
if (trades.length === limit) {
offset += limit;
return getTrades(limit, offset, query);
} else {
return allTrades;
}
})
.catch(function(e) {
console.log(e.stack);
})
}
return getTrades(limit, offset, query)
}
If you knew the total # of trades in advance you could use a different strategy with .map
and {concurrency: N}
to get N pages of trades at once.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…