michael643
michael643

Reputation: 247

Return global array with values setted in a function

I create another post but I didn't wrote exactly the proper code and what the problem is. So here the full code. I declare "myarray" in the create function. I push the values in my success function to the array and return in the create create function.

The problem is that I don't get any values back when call the create function. I think it's something the scope om my array but I don't know exactly how I can resolve this.

    function Create(targetdir)
    {
        var myarray = new Array(); 

        //Get a list of file names in the directory
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onSuccess, onError);

        function onSuccess(fileSystem) 
        { 
            var entry=fileSystem.root;      
            entry.getDirectory(targetdir, {create: false, exclusive: false}, successdir, fail);     

            //filesystem2 is the target dir
            function successdir(fileSystem2) 
            {           
                var directoryReader = fileSystem2.createReader(); 
                directoryReader.readEntries(success, fail); 

                function success(entries) 
                {

                    var i;
                    for (i=0; i<entries.length; i++) 
                    {
                        myarray.push(entries[i].toURI());
                    }           
                }
            }         
        }

         return myarray;
    }

Upvotes: 1

Views: 575

Answers (2)

Manuel van Rijn
Manuel van Rijn

Reputation: 10305

because you're calling a async method you're create method alwasy will return nothing, because the window.requestFileSystem is still doing his job. You could do following

function Create(targetdir)
{
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onSuccess, onError);

    function onSuccess(fileSystem) 
    { 
        var myarray = new Array(); 
    // fill myarray
    // use myarray or skip the fill array and use it directly
    }
}

or use the callback method as specified by @IAbstractDownvoteFactor

Upvotes: 0

Joe
Joe

Reputation: 82614

Use a callback:

function Create(targetdir, callback)
{
    var myarray = new Array(); 

    //Get a list of file names in the directory
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onSuccess, onError);

    function onSuccess(fileSystem) 
    { 
        var entry=fileSystem.root;      
        entry.getDirectory(targetdir, {create: false, exclusive: false}, successdir, fail);     

        //filesystem2 is the target dir
        function successdir(fileSystem2) 
        {           
            var directoryReader = fileSystem2.createReader(); 
            directoryReader.readEntries(success, fail); 

            function success(entries) 
            {

                var i;
                for (i=0; i<entries.length; i++) 
                {
                    myarray.push(entries[i].toURI());
                }           
            }
        }

        // call callbqack
        callback(myarray);    
    }
}

Then:

Create(whatever, function (myarray) {
  // do something with my array
}); 

Upvotes: 1

Related Questions