title: For loop; how to wait for next function to complete for dealing with next object? JavaScript
body: <p>Please can somebody help me; 
  I am using a for loop, which loops through each item in an array (document names), each item calls a function which uploads the corresponding document. </p>
  
  <p>My issue is that before the document has finished uploading, the for loop has moved onto the next item. How can I get my for loop to wait until the called function has finished before working on the next array item?</p>
  
  <p>Any help is very much appreciated.</p>
  
  <p>my code;</p>
  
  <pre class="lang-js prettyprint-override"><code>  fileNameArray = [doc1.doc, doc2.doc, doc3.doc];
    fileArrayLength = fileNameArray.length;
  
    loop = 0;
    for (fa = 0; fa &lt; fileArrayLength; fa++) {
      if (loop == fa) {
        multiupload()
      } else {
        console.log("not ready for " + fileNameArray[fa]);
      };
    };
  };
  
  function multiupload(fileName) {
    fileName = fileNameArray[fa];
    //code here uploads document
    loop++;
  };
  </code></pre>
  
  
  
  <p><strong>Edit</strong>
  I got this working with the following code;</p>
  
  <pre class="lang-js prettyprint-override"><code>function promiseFun() {
    let myPromise = new Promise((resolve, reject) =&gt; {
      multiUpload(fileNameArray[index]);
  
      function multiUpload() {
        //my upload code
        resolve("Success!");
      };
    });
    myPromise.then((successMessage) =&gt; {
      index++;
      if (index &lt; fileArrayLength) {
        promiseFun();
      };
    });
  };
  </code></pre>