title: Best way to write loops with promises (ctx.sync) in JavaScript API for Office
body: <p>There are many threads that discuss about guaranteeing execution order of promises in loops. I would like to know what is the best practice in JavaScript API for Office Add-ins. Most of the time, the promise in question is <code>ctx.sync()</code>.</p>
  
  <p>Here is a snippet to print the address of a list of Excel ranges one by one. The test shows that it respects well the order of Excel ranges. But the question is <strong>whether and how to guarantee</strong> the execution order?</p>
  
  <pre><code>function loadAll () {
     var ranges = ["A:A", "B:B", "C:C", "D:D", "E:E"];
     var sheet = "Sheet1";
     for (var i = 0; i &lt; ranges.length; i++) {
         loadRange(ranges[i], sheet);        
     }
  }
  
  function loadRange (range, sheet) {
     Excel.run(function (ctx) { 
         var r = ctx.workbook.worksheets.getItem(sheet).getRange(range);
         r.load('address');
         return ctx.sync().then(function() {
            console.log(r.address);
         });
     });
  }
  </code></pre>
  
  <p>Could anyone help?</p>