I am creating a browser-tool that is running on Chrome WebServer AddOn. The server offers a directory file listing by using GET
request returning in a JSON array (incl. isFolder/isFile/filename values)
I have a list of folders that I need to scan for all files in this folder, including all subdirectories that should be returned in pushArray.
Now usually I would use a recursive function to iterate through this. As I am using $.get
I have an asynchronous request and I have no idea how to iterate through then and return a list of all files. I tried around with .when().done()
but I think am just lacking the idea how to do it instead of recursion.
The following code is not working and I understand why but what I have done so far:
function recursiveFileRead(data, pushArray) {
var val = data; //data contains
val = String(val).trim() + "?json=1" //required so the server gives back the json object
//get list of files and folder in a json object (async!)
var callBack = $.get(val, function(val) {
json = val;
//loop through json object.
$.each(json, function(i, value) {
//if file push to array
if(value.isFile) {
pushArray(value.filename);
} else {
//if folder recursively run through the subfolders
recursiveFileRead(data, pushArray)
};
});
});
$.when( callBack() ).done(function() {
//return pushArray
});
}
After the array contains all files listed, I need to perform an action to ZIP those files. I guess I won't be able to do this synchronous, but if I iterate asynchronously how do I even know when I am finished?
Thanks a lot!