I want to run window.resolveLocalFileSystemURI(file,success,fail)
in for
loop passing different file entries and want to return resolved entries in arr
this is all based on all your functions being synchronous, and if not: be more specific what you're using. (there is no jQuery here yet your tags say jquery)
function resolveFile(path) {
var result;
window.resolveLocalFileSystemURI(path, function(file) {
result = file;
});
return file;
}
var resolvedFiles = filesarr.map(resolveFile);
callback(resolvedFiles);
There are multiple primary ways to attack a problem like this:
Here's the manual version:
function getFiles(filesarr, doneCallback) {
var results = new Array(filesarr.length);
var errors = new Array(filesarr.length);
var errorCnt = 0;
var overallCnt = 0;
function checkDone() {
if (overallCnt === filesarr.length) {
if (errorCount) {
doneCallback(errors, results);
} else {
doneCallback(null, results);
}
}
}
for (var i = 0; i < filesarr.length; i++) {
(function(index) {
window.resolveLocalFileSystemURI(url, function (entry) {
results[index] = entry;
++overallCnt;
checkDone();
}, function (e) {
errors[index] = e;
++errorCount;
++overallCnt;
checkDone();
});
})(i);
}
}
getFiles(filesarr, function(errArray, results) {
if (errArray) {
// go errors here
} else {
// process results
}
});
And, here's a version that uses ES6 promises:
// make a promisified function
function resolveFile(url) {
return new Promise(function(resolve, reject) {
window.resolveLocalFileSystemURI(url, resolve, reject);
});
}
function getFiles(filesarr) {
var promises = [];
for (var i = 0; i < filesarr.length; i++) {
promises.push(resolveFile(filesarr[i]));
}
return Promise.all(promises);
}
getFiles(filesarr).then(function(results) {
// process results here
}, function(err) {
// error here
});