0

The axios_simulation fucntion represents the api call. I want to loop 10 times and call the api in an interval of 1 second. But the api is called 10 times simultaneously after 1 second ([Done] exited with code=0 in 1.135 seconds) How to fix this problem?

function axios_simulation() {
  return new Promise(function(resolve, reject) {
    console.log("request the api");
    resolve('success');
  }
  );
}

function run_in_interval(interval) {
  for (var i = 0; i < 10; i++) {
    axios_simulation().then(function(result) {
      console.log(result);
    });
    sleep(interval);
  }
}

function sleep(milliseconds) {
  setTimeout(function() {
  }
  , milliseconds);
}

run_in_interval(1000);
ming
  • 427
  • 1
  • 5
  • 14

0 Answers0