0

In my code -

function longForLoop(limit) {
    var i = 0;
    var ref = setInterval(() => {
        console.log("\n-----------------------------\n");
      
      normUrl = "https://sih-utils.herokuapp.com/normal?a="+qActualNormal +"&b="+qIdeal;



      var obj;
      fetch(normUrl)
        .then(res => res.json())
        .then(data => obj = data)
        .then(() => console.log(obj))
      console.log(obj);


   
        
      i++;
      if (i == limit) {
          clearInterval(ref);
      }
    }, 2000);
  }
  
  longForLoop(2);

Following result is obtained -

enter image description here When I run this code, it says obj not defined, I have 2 more similar calls to make to the same API below this and it returns some result that is stored in a variable, and in the end, I want to console.log the results obtained according to the order in which they were called.

1 Answers1

0

you probbly want to do something like this,

fetch(normUrl)
        .then(res => res.json())
        .then(data => console.log(data))

console.log(obj);

The above err may be caused by many reason, one of which it you might not have obj defined previously

ezio4df
  • 3,541
  • 6
  • 16
  • 31