title: executeQueryAsync synchronously in each loop for sharepoint javascript model
body: <p>I am performing looping in javascript for sharepoint. I want it to perform synchronously, means only after first data execution is done it should start with second. If first data execution is completed (whether it success or fail) then only it should start with second in each loop. </p>
  
  <pre><code>$.each(selectedSiteCollections, function (index, value) {
  
                   act.SharePoint.SharePointAppProgress.addStatusRow(act.Resources.StatusInfo.applyFolderNavigation, "applyFolderNavigationId");
                   var siteUrl = value;//_spPageContextInfo.siteServerRelativeUrl + "/" + projectUrl;
                   var  clientContext = act.SharePoint.SharePointAppCommon.getClientContext(siteUrl);
                   var newWebsite = clientContext.get_web();
  
                   var pageUrl = siteUrl + "/Shared%20Documents/Forms/AllItems.aspx";
                   var homePageUrl = siteUrl + "/Pages/Home.aspx";
  
                   var jsLinkUrl = '~siteCollection/_catalogs/masterpage/FolderNavigation.js'; //&lt;-- specify JSLink url
  
                   var page = newWebsite.getFileByServerRelativeUrl(pageUrl);
                   var homePage = newWebsite.getFileByServerRelativeUrl(homePageUrl);
  
  
  
                   var wpm = page.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
                   var collWebPart = wpm.get_webParts();
  
                   var wpmHome = homePage.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
                   var collWebPartHome = wpmHome.get_webParts();
  
                   clientContext.load(homePage);
                   clientContext.load(collWebPart, 'Include(WebPart.Properties)');
                   clientContext.load(collWebPartHome, 'Include(WebPart.Properties)');
  
                   clientContext.executeQueryAsync(function () {
                       if (homePage.get_checkOutType() != SP.CheckOutType.online) {
                           homePage.checkOut();
                       }
                       var webPartDef = null;
                       for (var x = 0; x &lt; collWebPart.get_count() &amp;&amp; !webPartDef; x++) {
                           var temp = collWebPart.get_item(x);
  
                           webPartDef = temp;
  
                       }
                       if (webPartDef != null) {
                           var webpart = webPartDef.get_webPart();
  
  
                           var properties = webpart.get_properties();
  
                           properties.set_item('JSLink', jsLinkUrl);
                           webPartDef.saveWebPartChanges();
                       }
  
  
                       var webPartDefHome = null;
                       for (var x = 0; x &lt; collWebPartHome.get_count() &amp;&amp; !webPartDefHome; x++) {
                           var temp = collWebPartHome.get_item(x);
  
                           webPartDefHome = temp;
  
                       }
                       if (webPartDefHome != null) {
                           var webpart = webPartDefHome.get_webPart();
  
  
                           var properties = webpart.get_properties();
  
                           properties.set_item('JSLink', jsLinkUrl);
                           webPartDefHome.saveWebPartChanges();
                       }
  
  
                       homePage.checkIn("ACT Update webpart", SP.CheckinType.majorCheckIn);
                       homePage.publish("ACT Update webpart");
                       clientContext.executeQueryAsync(function () {
  
                           act.SharePoint.SharePointAppProgress.updateStatusRow(true, "applyFolderNavigationId");
  
                       }, function (sender, args) { executeAsyncFailed(sender, args, "applyFolderNavigationId"); });
                   }, function (sender, args) { executeAsyncFailed(sender, args, "applyFolderNavigationId"); });
  
               });
  </code></pre>