Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I know when the last async is done?

I have something like this:

for (var i=0;i<result.qry.ROWCOUNT;i++) {
  myAsync(i);
}

How do I know when all my Async functions have finished executing?

At the risk of someone replying with "Needs more jQuery!", can I use the jQuery promise object? Or deferred or something like that?

like image 260
Phillip Senn Avatar asked Jan 24 '12 19:01

Phillip Senn


People also ask

How do you know when async is completed?

Async function won't technically returns an indicator for "Completed" before it completes. If you have the control for the service controller, you can send to client side a response size first and for each data chunk you send to the client side, you can make your own progress bar to see when it will complete.

Does async function return immediately?

Async functions don't return immediately, they run the body of the code until the hit an await and return a promise. In your example, the entire function will run before it returns.

Does async await stop execution?

Description. The await expression causes async function execution to pause until a Promise is settled (that is, fulfilled or rejected), and to resume execution of the async function after fulfillment.

What happens when you call an async function?

In this article The current method calls an async method that returns a Task or a Task<TResult> and doesn't apply the Await operator to the result. The call to the async method starts an asynchronous task. However, because no Await operator is applied, the program continues without waiting for the task to complete.


3 Answers

Keep track of how many asynchronous calls are outstanding. When each finishes, decrement your counter. When you get to 0, you are in the last callback.

var asyncsLeft = 0;
for (var i=0;i<10;++i){
   asyncsLeft++;
   doSomethingAsyncWithCallback(function(){
     // This should be called when each asynchronous item is complete
     if (--asyncsLeft==0){
       // This is the last one!
     }
   });
}

Due to the single-threaded nature of JavaScript there is no potential race condition where you might get your callback invoked before all of the asynchronous calls have been queued up. It is safe to put the asyncsLeft++ call after the doSomethingAsynchronous, if you like.

like image 139
Phrogz Avatar answered Sep 22 '22 10:09

Phrogz


This is how I would do it:

//Do stuff up here to get records
var rowCount = result.qry.ROWCOUNT, //Save the row count
    asyncCount = 0, //The count of complete async calls
    asyncCallback = function() {
        //To be called whenever an async operation finishes
        asyncCount++; //Increment the row counter
        if (asyncCount >= rowCount) {
            //Do stuff when they're all finished
        }
    };

for (var i=0;i<rowCount;i++) {
  myAsync(i, asyncCallback);
}

function myAsync(index, completeCallback) {
    //Do async stuff with index
    //Call completeCallback when async stuff has finished or pass it
    // into the async function to be called
}
like image 26
Richard Marskell - Drackir Avatar answered Sep 20 '22 10:09

Richard Marskell - Drackir


In jQuery, there is the $.ajaxStop function that runs after the last Ajax has ran.

like image 40
Michael Irwin Avatar answered Sep 23 '22 10:09

Michael Irwin