I am calling a function from my AngularJS controller. When the function completes execution, I would like to print the result after the function call.
This code is inside the controller, which is called on-tap
//count the number of files inside a directory
var count = countDir(cordova.file.dataDirectory +'/Images/');
console.log('Hello ');
console.log('Count of directory '+count);
Here is the countDir function. This function finds the number of files in a directory and then returns the count back
function countDir(path){
var count = 0;
console.log('Counting the files inside '+path);
window.resolveLocalFileSystemURL(path,
function (fileSystem) {
var reader = fileSystem.createReader();
reader.readEntries(
function (entries) {
var i;
for (i=0; i<entries.length; i++) {
console.log('File name ' + entries[i].name + ' added to count');
count++;
}
},
function (err) {
console.log(err);
}
);
}, function (err) {
console.log(err);
}
);
return count;
}
The challenge that I am facing is that, my calling code first prints 'Hello' and 'Count of directory 0' and then everything inside countDir is printed.
Is the call to countDir()
asynchronous? If so, how can I ensure that my calling code proceeds once countDir()
has returned a result.