Astronaut
Astronaut

Reputation: 7051

Functional programming issue regarding passing functions with parameters

I am currently writing an API to unzip files to the web browser sandbox filesystem. I have a basic need to pass a parameter to a function that in turn is itself passed as a parameter. Here is some code to illustrate the issue:

//Request a permanent filesystem quota to unzip the catalog.
function requestFilesystem(size){
    window.webkitStorageInfo.requestQuota(PERSISTENT, size*1024*1024, function(grantedBytes) {
        window.requestFileSystem(PERSISTENT, grantedBytes, function(fs) {
            filesystem = fs;
            removeRecursively(filesystem.root, unzip(url), onerror);
        }, onerror);
    }, function(e) {
        console.log('Error', e);
    });
}

//unzip method can be changed, API remains the same.
//URL of zip file
//callback oncomplete
function unzip(URL) {
    importZipToFilesystem(URL, function(){
        console.log("Importing Zip - Complete!");
    });
}

//remove removeRecursively a folder from the FS
function removeRecursively(entry, onend, onerror) {
    var rootReader = entry.createReader();
    console.log("Remove Recursive"+entry.fullPath);
    rootReader.readEntries(function(entries) {
        var i = 0;

        function next() {
            i++;
            removeNextEntry();
        }

        function removeNextEntry() {
            var entry = entries[i];
            if (entry) {
                if (entry.isDirectory)
                    removeRecursively(entry, next, onerror);
                if (entry.isFile)
                    entry.remove(next, onerror);
            } else
                onend();
**Uncaught TypeError: undefined is not a function**
        }

        removeNextEntry();
    }, onerror);
}

If I try to use

function removeRecursively(entry, onend(URL), onerror) { 

there is an error, ao my issue is how to pass around the URL value for the unzip function, this unzip function is used as a callback function on the onsuccess of removeRecursively

Upvotes: 0

Views: 137

Answers (2)

Paweł Chorążyk
Paweł Chorążyk

Reputation: 3643

You are calling

removeRecursively(filesystem.root, unzip(url), onerror);

but unzip doesn't return anything

function unzip(URL) {
    importZipToFilesystem(URL, function(){
        console.log("Importing Zip - Complete!");
    });
}

So the second argument of removeRecursively (onend) becomes undefined, which probably causes the error when you trying to use it as a function.

If you want to use unzip function as a callback, you should just pass unzip (instead of unzip(url)) without calling it and then call onend(URL) inside removeRecursively.

Upvotes: 1

Jad
Jad

Reputation: 932

You're passing the result of unzip to removeRecursively, which is undefined.

What you probably want to do is

removeRecursively(filesystem.root, function() { unzip(url); }, onerror);

Here you pass a function as a parameter, this function calls unzip with the parameter you want.

Upvotes: 3

Related Questions