1

so what i want to do is to keep trying to get the data check if the value that i want does exist if it doesn't exist it would return an error and then re do the action until the value does exist. so what i have till now this is the https call

test = async() => {
  console.log("Hellow")
  now = new Date();
  const https = require("https");
  https.get("website",{ agent: proxyy },
    (res) => {
      var body = "";
      res.on("data", function (chunk) {
        body += chunk;
      });
      res.on("end", function () {
        var resp = JSON.parse(body);
        data_wanted = resp.data
        const desiredItem = data_wanted.find((item) =>
          item.name.includes("data")
        );
        console.log(desiredItem)
      });
    }
  );
};

what i did but didn't worked out

while (flag = true ){
  let flag = false 
  try{
  await test()// test is the same function above
}catch(e){
  flag = true
}

}

so the try catch dont catch any error so the flag wont change to true , and neither the while loop work alone for some reason

EDIT1 so what i think is happening since https is async the try/catch is running before the https call is finished.

3 Answers3

4
async function test(){
   // log after 5 seconds
   setTimeout(()=>{console.log("ran after 5 sec")},5000)
   // and also return a string
   return "helloReturn";
}

async function infiniteLoop(){
// a while loop that never ends
   while(true){
      await test();
   }
}
infiniteLoop() // call the infinteLoop function

This code will never log anything because asynchronous calls results are only executed when the main thread is free. I would recommend you, check out this article for more info. https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/ Talking about a solution what you can do is pass your function in setInterval and check the result for that function if you found the value than clear the interval. Let me know if you need the code for it. so here is the code, I think it's not optimized and the right way to do but should work

interval = setInterval(test,1000) //  set interval for 1 sec function would be hoisted
async function test(){
 console.log("Hello")
  now = new Date();
  https.get("website",{ agent: proxyy },
    (res) => {
      var body = "";
      res.on("data", function (chunk) {
        body += chunk;
      });
      res.on("end", function () {
        var resp = JSON.parse(body);
        data_wanted = resp.data
        const desiredItem = data_wanted.find((item) =>
          item.name.includes("data")
        );
        console.log(desiredItem)
      });
    }
  );
    if(desiredItem)
        clearInterval(interval)
}

If you need to do some other things after getting this result you can emit an event in the if statement and listen to that event in your code. this, You have to do. check event emitter class in the nodejs.

Namit Piriya
  • 141
  • 7
2

i think you forget to throw error after find 'data'. maybe you need something like this

const desiredItem = data_wanted.find((item) =>
      item.name.includes("data")
 ); 
if (!desiredItem) throw Error('data not found');
return desiredItem

and on execute function you can use just try catch, maybe something like this

_execute = async () => {
  try {
   await test()
  } catch(e) {
    await _execute()
  }

}

and call the _execute func

return _execute();

you also forget add await on https.get, you need to do this.

await https.get()
syns
  • 41
  • 1
  • 2
2

The accepted answer is a clever answer just want to give mine since is a bit simpler to understand

test2 = async() => {
  console.log("Hellow")
  
  now = new Date();
 res =  await https.get("website",
  // { agent: proxyy },
    (res) => {
      // console.log(res)
      var body = "";
      res.on("data", function (chunk) {
        body += chunk;
      });
      res.on("end", function () {
        var resp = JSON.parse(body);
        data_wanted = resp

       
        const desiredItem2 = data_wanted.find((item) =>
          item.name.includes("data")
        );
      
       try{
        console.log(desiredItem2)
      }catch(e){
        console.log("couldnt find the id")
        test2()
      }
      });
    }
  )
}
Taha Daboussi
  • 350
  • 3
  • 14