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();
};
});
};