I'm developing a simple function that, making use of superagent module, query an API service to retrieve information splitted in several pages.
As soon as I finish each call I have to push the information into an array and then as I retrieved the last page, start working on this array.
Using a normal way for sure I'll have problem due to async-ness, so I need something like callback or event emitter to get rid of this.
Atm I used this code, but this doesn't work:
function getPage(page){
pages = new Array()
superagent.get('http://localhost/api.json')
.end(function(r){
if(r[r.length-1] == 'value')
getPage(page++)
else
pages.push(r);
);
}
Take a look at async.js, it's a really useful library that can help you with this.