Home > Enterprise >  NodeJS : While loop check the status untill sucess in fetch
NodeJS : While loop check the status untill sucess in fetch

Time:04-20

I have the below code which call api and get status using node-fetch . I need to use node-fetch 2.6.1 and use the code like .then-->.then cannot able to use async await .

function fetching(){
 fetch(jobsurl, requestOptions)
    .then(response => response.text())
    .then(result => {
        
        console.log("------------the job id is ------------"); 
        console.log(eval(result)[0]["id"])
        console.log("\n\n\n\n\n")
        console.log("--------------The Job Status is------------");
        console.log(eval(result)[0]["status"])
        console.log("\n\n\n\n\n")
        var job_id=eval(result)[0]["id"].toString();
        var stat=eval(result)[0]["status"];

       while(stat != "success"){
           
         fetch(jobsurl, requestOptions)
           .then(response => response.text())
           .then(result => {
               console.log("------------the job id is ------------"); 
               console.log(eval(result)[0]["id"])
               console.log("\n\n\n\n\n")
               console.log("--------------The Job Status is------------");
               console.log(eval(result)[0]["status"])
               console.log("\n\n\n\n\n")
               job_id=eval(result)[0]["id"].toString();
               stat=eval(result)[0]["status"];
            }
           // as it is success so call the main url
           fetch()....             

           }//ftehcing end

now it has stat variable inside then that give the job status success or pending if it sucess then only I can proceed. But for that I need to iterate and check when it will get success every time. so I used while loop.

But it is not working. as stat value check every time by calling fetch inside it.

Please suggest

CodePudding user response:

Here's an async/await reworking of your code that makes the loop pretty much trivial.

There's also

  • proper error handling (using response.ok)
  • no use of evalresponse.json() should do what you want
  • a delay between requests (so as not to spam the server milliseconds apart)
function delay(number) {
  return new Promise(resolve => setTimeout(resolve, number));
}

async function fetching() {
  while (true) {
    const response = await fetch(jobsurl, requestOptions);
    if (!response.ok) {
      throw new Error(`HTTP error, status = ${response.status}`);
    }
    const results = await response.json();
    const {id, status} = results[0];
    console.log(`Job ${id} status: ${status}`);
    if (status === 'success') {
      break;
    }
    await delay(500);  // Wait for a bit before re-requesting
  }
  // Call main url..?
}

EDIT: If you absolutely can't use async/await, you'd need to use a recursive function, something like this, and it's pretty hard to read.

function delay(number) {
  return new Promise((resolve) => setTimeout(resolve, number));
}

function fetching() {
  function _recursive() {
    return fetch(jobsurl, requestOptions)
      .then(response => {
        if (!response.ok) {
          throw new Error(`HTTP error, status = ${response.status}`);
        }
        return response.json();
      })
      .then(results => {
        const {id, status} = results[0];
        console.log(`Job ${id} status: ${status}`);
        if (status === 'success') {
          return results;
        }
        return delay(500).then(_recursive);
      });
  }
  return _recursive();
}

function main() {
  fetching().then(results => {
    console.log(results);
    // call main URL..?
  });
}
  • Related