0

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.

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?

Any help is very much appreciated.

my code;

  fileNameArray = [doc1.doc, doc2.doc, doc3.doc];
  fileArrayLength = fileNameArray.length;

  loop = 0;
  for (fa = 0; fa < 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++;
};

Edit I got this working with the following code;

function promiseFun() {
  let myPromise = new Promise((resolve, reject) => {
    multiUpload(fileNameArray[index]);

    function multiUpload() {
      //my upload code
      resolve("Success!");
    };
  });
  myPromise.then((successMessage) => {
    index++;
    if (index < fileArrayLength) {
      promiseFun();
    };
  });
};
mplungjan
  • 169,008
  • 28
  • 173
  • 236
John
  • 19
  • 6
  • You can't use a for loop. You need a recursive function. – Sergiu Paraschiv Mar 27 '17 at 09:36
  • You have an async function - you need to call again in the callback – mplungjan Mar 27 '17 at 09:37
  • Don't use loop. Since uploading is an async function you will have to deal with promises to achieve the required task. – Apoorv Joshi Mar 27 '17 at 09:38
  • If you could add the code that uploads the document, someone might be able to give you a hint how to implement it recursively. – loafer Mar 27 '17 at 09:39
  • 1
    @jamesDonelly - very poor duplicate. This is not a recursive function but a set of async ones. This is a better duplicate: http://stackoverflow.com/questions/13656066/html5-multiple-file-upload-upload-one-by-one-through-ajax – mplungjan Mar 27 '17 at 09:41
  • Thank you all for your comments, im not sure that the links given were what I wanted. I managed to make it work using Promise, thank you to @Dhiraj for pointing me in the right direction. – John Mar 27 '17 at 10:49

1 Answers1

0

You can try it using promise as below

index= 0;

multiUpload(){
var Promise = new Promise(function(resolve, reject) {
  multiuploadPromise(fileNameArray[index]);
}

Promise.then(function (data) {
  index++;
  if(index < fileArrayLength){
   multiUpload();
  }
}
}
Dhiraj
  • 1,430
  • 11
  • 21
  • Thank you @Dhiraj although your code didn't work exactly, it put me on the right track and I managed to make it work using: function promiseFun(){ let myPromise = new Promise((resolve, reject) => { multiUpload(fileNameArray[index]); }); myPromise.then((successMessage) => { index++; if(index < fileArrayLength){ promiseFun(); }; }); }; – John Mar 27 '17 at 10:48
  • it was just a sample. But you got it right. Hope your problem got solved – Dhiraj Mar 27 '17 at 10:55
  • I will update my answer – Dhiraj Mar 27 '17 at 10:57