0

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?

5
  • Where is it failing? Commented Jul 4, 2017 at 11:04
  • In above example it never failing, it just keep looping. Commented Jul 4, 2017 at 11:08
  • Put your loop++ outside all the if else conditions. Commented Jul 4, 2017 at 11:12
  • You are checking the value of APIResult, which seems to be async method. use a callback and then a timeout maybe to wait for sometime before hitting the API again. Commented Jul 4, 2017 at 11:19
  • @Chandra Eskay putting loop++ outside didn't work. Commented Jul 4, 2017 at 11:23

1 Answer 1

1

Your APIResult function doesn't return anything. This function is asynchronous, so it should return promise or use a callback.

Your code result = APIResult(rarray, roads); sets to result variable undefined value. I think, async/await style for implementing asynchronous JS features will work for you.

Current last Node.js version is 8.1. It has native support for async/await. There is an example, how you can implement your code:

async function main() {
  var roads = 1000;
  var findResult = true;
  var loop = 0;
  while (findResult) {
    try {
      result = await APIResult(rarray, roads);
    } catch (e) {
      //APIResult reject promise branch
    }

    if (result !== null) {
      findResult = false; // stop the loop
    } else if (loop == 5) {
      findResult = false; // stop the loop
    } else {
      roads = roads * 10;
      loop++;
    }
  }
}

async function APIResult(rarray, roads) {
  return new Promise((res, rej) => {
    request.post(
        env.TEST_URL + 'test/',
        {
          json: {
            //...
            roads: roads,
            //..
          },
        },
        function(error, response, body) {
          if (error) return rej(error);
          if (response.statusCode === 200) return res(JSON.parse(body.rows[0].result));
        });
  });
}

main();
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your reply @galkin, then above code won't work in the node v6.11.0?
Tried your code, but for some reason there is no records returning now from the API response. I even placed a debug code in the request call back function and looks like it's not even reaching there.
Yes, this code will not work on 6.11 Node.js without babel (babeljs.io). As i wrote, for you will be the best solution use async/await. Or you should learn Promises/callbacks/generators staff.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.