B
B
Bur Ov2018-01-29 01:23:39
JavaScript
Bur Ov, 2018-01-29 01:23:39

How to make a loop wait for a function?

Good afternoon. There are 2 functions:

function showUser(data) {
  for (var k = 0; k < 10; k++) {
    arrayMess = [];
    getMes(data[k][0],data[k][0] );
  }
}
function getMes(data1,data2) {
      var i = 200;
      var timerId = setInterval(function() {
        addScript('https://api.vk.com/method/execute?code=' + encodeURIComponent('var mes = API.messages.getHistory({"user_id":'+ data1 +',"count": 200});var i = '+ i +';var arr = [];arr = arr + [email protected]_id;while (i < ('+ i +' + 4800) ) {var mes = API.messages.getHistory({"offset":i,"user_id":'+ data1 +',"count": 200});arr = arr + [email protected]_id;i = i + 200;}return {"count":mes.count,"users":arr};') + '&access_token=' + token + '&callback=showMess&v=5.69');
        if (data2 == arrayMess.length) { 
          clearInterval(timerId);
        }
      i = i + 5000;
      }, 1000);	
}

After the showUser function is launched, the cycle starts to work and pull another function, now for some reason it immediately works out and calls a bunch of functions, but it is necessary that it waits for the function that it launches to work and only then continues, how to do this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2018-01-29
@burov0798

With async/await you can do this: https://jsfiddle.net/rockon404/ewpbd4bv/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question