Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9101673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:11:29+00:00 2026-06-17T01:11:29+00:00

I am looping through a small array of file names to grab from a

  • 0

I am looping through a small array of file names to grab from a remote server and save locally. This bit works OK.

The problem is, I need to run my next function when the downloads are complete.

Currently, my function, lets call it step2(), gets called before all the FileStreams can finish (infact, its called before that function even starts!)

I read that there is a complete event listener when using openAsynch, but I need to check that all files, e.g. 3 remote swfs) have been written and then start step2() function.

How can I go about this?

Code below:

function onPlaylistComplete(e:Event)
{
    var myArray:Array = new Array();
    for each(var i in list_of_files)
    {
        myArray.push(i);
    }

    for(i = 0; i < myArray.length; i++)
    {
        swfItem = myArray[i];
        var urlString:String =  site_url + myArray[i];
        var urlReq:URLRequest = new URLRequest(urlString);
        var urlStream:URLStream = new URLStream();

        urlStream.addEventListener(Event.COMPLETE, urlStreamComplete(swfItem));
        urlStream.load(urlReq);
    }

    function urlStreamComplete(swfItem):Function
    {
        var downloadSwf:Function = function (event:Event):void {
            var fileData:ByteArray = new ByteArray();
            var stream = event.target;
            stream.readBytes(fileData, 0, stream.bytesAvailable);
            try {
                var f : File = File.documentsDirectory.resolvePath(swfItem);
                var fs : FileStream = new FileStream();
                fs.addEventListener(Event.COMPLETE, fileCompleteHandler);
                fs.openAsync(f, FileMode.WRITE);
                fs.writeBytes(fileData);
            }
            catch (err : Error) {
                trace(err.message);
            }
        }
        return downloadSwf;
    }

    function fileCompleteHandler(e:Event):void {
        e.target.close();
    }

    step2(); // this seems to run before UrlStreamComplete()
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T01:11:31+00:00Added an answer on June 17, 2026 at 1:11 am

    Problem with your for loop it will never work with Async model. so better need work to with recursive function.

    downloadAllFiles method second arg that is function called when all download are completed.Then you can call step2() or whatever may be.

    Make sure if any problem while download file like IOErrorEvent,etc…

        downloadAllFiles(myArray,function():void
        {
            step2();
        });
    
        function downloadAllFiles(myArray:Array, complete:Function):void
        {
            if(myArray && myArray.length == 0)
            {
                if(complete)
                    complete.call();
                return;
            }
    
            swfItem = myArray.shift();
    
            var urlString:String =  site_url + swfItem;
            var urlReq:URLRequest = new URLRequest(urlString);
            var urlStream:URLStream = new URLStream();
    
            urlStream.addEventListener(Event.COMPLETE, function (event:Event):void 
            {
                var fileData:ByteArray = new ByteArray();
                var stream = event.target;
                stream.readBytes(fileData, 0, stream.bytesAvailable);
    
                try {
                    var f : File = File.documentsDirectory.resolvePath(swfItem);
                    var fs : FileStream = new FileStream();
    
                    fs.addEventListener(Event.CLOSE, function(closeEvent:Event):void
                    {
                        downloadAllFiles(myArray,complete);
                        return;
                    });
                    fs.openAsync(f, FileMode.WRITE);
                    fs.writeBytes(fileData);
                }
                catch (err : Error) 
                {
                    trace(err.message);
                }
            });
    
            urlStream.load(urlReq);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looping through an array of file paths retrieved by a core data
Looping through each id retrieve value of objects in 1 dimentional array and 2
Must be a way looping through this code: private void loadSprites() { this.sprites[0] =
I am looping through a large text file and im looking for lines that
I'm looping through an associative array with a foreach. I'd like to be able
So, I have this small bit of code that generates rows and columns of
I got interested in this small example of an algorithm in Python for looping
I am looping through a big data file and like to detect the type
Im about to start a small project using OpenGL and Java. Looking through this
While looping through a Dataset which code snippet should I use, should I go

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.