title: How to block calling function while multiple asynchronous calls are executed, then contiune processing the collected data?
body: <p>I would like to process an XML file in <b>JavaScript</b>, read every sheet asynchronously (because oviously it would be faster), and return the final result. I have the parser functions, I got stuck on the part when I would like to wait for every async call to finish, then return the result. A callback isn't the best practice for me in this case, because this function would be called at the beggining of another module, where I wouldn't like to put the rest of the code (let's say for example 500 lines) in a callback. </p>
  
  <p>I have tried many approaches, the last one looks like the one below. But it seems to result in an infinite loop. All the other attempts returned undefined value, because the caller function ended before async calls would finish, and the accumulator haven't got any value.<br/><br/>
  1. To collect every async call's result, I used <b>promise</b>.<br/></p>
  
  <pre><code>function getAllSheets(callback) {
      accumulator = {};
  
      promise.all([
          processOneSheetAsync(workbook, sheetname1, ..., callback(data) {
              accumulator.one = data;
          }),
          processOneSheetAsync(workbook, sheetname2, ..., callback(data) {
              acc.two = data;
          }),
          /* some more function calls */
      ]).then(function(result) {
          callback(acc);
      });
  }
  </code></pre>
  
  <p>Note: <code>processOneSheetAsync()</code> is function that returns a <code>new Promise</code> and calls the <code>parser()</code>, as expected.</p>
  
  <p><br/>
  2. Then I tried to wait for it's callback in a function like this:</p>
  
  <pre><code>function getResult() {
      var result;
      var callback = false;
  
      getAllSheets(function(data) {
          result = data;
          callback = true;
      });
  
      while(!callback){};
      return result;
  }
  </code></pre>
  
  <p>I thought this would block the <code>getResult()</code> function until the variable <code>callback</code> is set to true. However, it seems that the <code>while</code> loop never detects the change of <code>callback</code>'s.</p>
  
  <p>Is there any good approach to solve this problem?</p>