0

I am writing a javascript HTML5 phonegap application. I am using SQLite to store data. For the sake of performance, i am undertaking database inserts asynchronously i.e. I do not need to call the next operation at the callback of the previous Database operation. Internally, i believe javascript is creating a worker for each operation hence multi-threading so to speak.

The problem now is , How do i know that all workers have completed their tasks? e.g, in order to tell the user that all data has been saved?

2
  • It should have a callback function on completion event Commented Jan 11, 2012 at 18:57
  • Thanks for the response Jani...actualy from my description,I am avoiding that because it would force me into some sort of serial operation where one thing must happen after another. Well, one way i am considering is to keep count of number of operations I start and decrement at completion....but if we had a way of knowing all workers still running, at any instance, it would be better.. Commented Jan 11, 2012 at 20:20

1 Answer 1

1

If I understand your request correctly, you are queueing up DB inserts to run asynchronously and you want to be able to check back at a later time to see if all the requests are finished. I would do something like this:

function asyncTask() {
        //
        // Do real work here
        //
        runningTasks--
}


//in your init section, setup a global variable to track number of tasks    
runningTasks = 0

//when you need to create a new task, increment the counter
runningTasks++;
setTimeout (asyncTask,1);



if (runningTasks > 0) {
    //something still running
} else {
    //all tasks are done.
}

In another language you would need to worry about race conditions when testing and setting the runningTasks varaible, but AFAIK, Javascript is only implemented as single threaded so you don't need to worry about that.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.