I'm trying to split an API request with an offset variable in order to have partial results without waiting the end of the entire request. Basically I make an API request for the first 100 values, and then I increase it with 100 more till reach the end. The offset is just the starting point.
/*Node.js connector to Context.io API*/
var key = xxxxxx;
var secret = "xxxxxxxxx";
var inbox_id = "xxxxxxxxx";
var end_loop = false;
var offset = 6000;
/*global ContextIO, console*/
var ContextIO = require('contextio');
var ctxioClient = new ContextIO.Client('2.0', 'https://api.context.io', { key: key, secret: secret });
while(end_loop === false) {
contextio_request(offset, function(response){
if (response.body.length < 100) { console.log("This is the end "+response.body.length); end_loop = true; }
else { offset += 100; }
console.log("Partial results processing");
});
};
/* Context.io API request to access all messages for the id inbox */
function contextio_request(offset, callback) {
ctxioClient.accounts(inbox_id).messages().get({body_type: 'text/plain', include_body: 1, limit: 100, offset: offset}, function (err, response) {
"use strict";
if (err) {
return console.log(err);
}
callback(response);
});
};
What I don't understand is why if I change the "while loop" with a "if condition", everything works, but with the "while" it enters in an infinite loop". Also, is it the correct way to make a partial request -> wait for response - > process the response -> follow with next request?