at.
at.

Reputation: 52520

Thread-safe queue in Javascript or jQuery

I have many asynchronous AJAX calls whose results will get processed. It doesn't matter what order the processing occurs, but the results need to get processed one at a time. So I'd like to simple do my AJAX calls and they all just put their results in a single queue. That queue should then get processed on a single thread. This way the results get processed one at a time as soon as possible.

What's the best way to do this? I'm using jQuery, so happy to take advantage of any facilities it provides for this.

Upvotes: 12

Views: 14174

Answers (4)

Josh Smeaton
Josh Smeaton

Reputation: 48720

Asynchronous does NOT mean 'multiple threads'. Think about many click events firing in a row, before the first click handler is processed. Only one action can be handled at a time, and the others will wait to execute.

Event driven languages like Javascript operate on the basis of a queue. Javascript in the background essentially has one giant queue that events and asynchronous responses get pushed in to. Once a particular piece of processing is complete, the next item from the queue is worked on.

These queues are sometimes known as 'Runloops'. Javascript will spin in an endless loop, retrieving an event from the queue, processing it, and returning back to the queue for another piece of work.

Multithreading can be achieved in (newer) versions of Javascript using Web Workers, but these are explicitly opt-in. Look them up if you're interested.

To answer your question then, just attach a callback to your asynchronous request, and it will complete processing even if another response is returned half way through. The other response will 'wait' until the current event is handled.

Upvotes: 27

Gutzofter
Gutzofter

Reputation: 2023

You might want to take a look at Event messaging between JavaScript objects.

I used an implementation similar to the blog post with a jQuery Plugin called ajaxMonitor.

The way it works as soon as any of the callbacks from the AJAX request get called it updates a HTML table. The callbacks happen asynchronous.

The very nature of AJAX means asynchronous. So processing your QUEUE in the success callback of your AJAX request, I believe would accomplish what your looking for.

Upvotes: 1

beeglebug
beeglebug

Reputation: 3542

The simplest way would be to implement your own queue system for the callbacks (using push/pop to emulate a stack).

As each asynchronous request returns, add the data to the end of an array and fire off some kind of queue.process() command, which would handle the first element in the array.

Finally, add something like if(alreadyRunning) { return; } to the top of that function to stop it doing more than one at once, and you've got your single threaded queue.

edit: removed code, some people were getting hung up on it instead of the original question

Upvotes: 0

Kyle Wild
Kyle Wild

Reputation: 8915

If your only concern is the browser (since you mention jQuery, I assume that's true), you should know that Javascript is single-threaded in browsers.

Upvotes: 5

Related Questions