I trying to loop through same API result and if the API result is NULL then I want loop through it again few times (i.e 4-5 times) with different parameters and if it's reached the 5th time. I want to exit the loop. The code I'm trying is see below:
var roads = 1000;
var findResult = true;
var loop = 0;
while (findResult) {
result = APIResult(rarray, roads);
if (result !== null) {
findResult = false; // stop the loop
} else if (loop == 5) {
findResult = false; // stop the loop
} else {
roads = roads * 10;
loop++;
}
}
function APIResult(rarray, roads) {
request.post(
env.TEST_URL + 'test/',
{
json: {
//...
roads: roads,
//..
},
},
function(error, response, body) {
if (!error && response.statusCode == 200) {
return JSON.parse(body.rows[0].result);
}
});
}
I'm even tried adding Q promise but didn't worked, any idea how to do it?