Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript parallelism

Well, first I want to say I'm a bit new in the world of Internet dev.

Anyway, I'm trying to know if its possible to run two pieces of code in parallel using javascript.

What I really need is to call two methods that are in a remote server. I pass, for both, a callback function that will be executed soon the data I want is ready. As the server running these functions take a time to answer, I'm trying to find a way to call both methods at the same time without need to wait till the first finishes to call the second.

Does methods like setTimeout run concurrently, for example

setTimeout(func1, 0);
setTimeout(func2, 0);

...

function func1()
{
   webMethod1(function() {alert("function 1 returned"); } );
}

function func1()
{
   webMethod2(function() {alert("function 2 returned"); } );
}

Edited

I've just found this article that may be very cool for the realease of next browsers: Javascript web workers

like image 941
Andres Avatar asked Jan 13 '10 14:01

Andres


3 Answers

There is one single thread of execution in Javascript in normal WebBrowsers: your timer handlers will be called serially. Your approach using timers will work in the case you present.

There is a nice piece of documentation on timers by John Resig (author of the very popular jQuery javascript framework - if you are new to Web development, I would suggest you look it up).

Now, if you are referring to HTML5 based browsers, at some point, they should have threading support.

like image 189
jldupont Avatar answered Oct 21 '22 03:10

jldupont


Yes, that's exactly how web requests through AJAX work. No need to setTimeout to 0, you can just call them one by one, and make an AJAX request, and it'll be executed asynchronously, allowing you to pass a callback function to be invoked when the request completes.

The means of creating an AJAX request differs some depending on what browser you're running. If you're going to build something that depends considerably upon AJAX, and you want it to work across multiple browsers, you're best off with a library. Here's how it's done in jQuery, for instance:

$.ajax({ url: '/webrequesturl', success: function(result) {
    // this will be called upon a successful request
} });
$.ajax({ url: '/webrequest2url', success: function(result) {
    // this will be called upon a successful request
    // this may or may not be called before the above one, depending on how long it takes for the requests to finish.
} });
like image 2
David Hedlund Avatar answered Oct 21 '22 02:10

David Hedlund


Well, JavaScript is single-threaded, the two timers will run sequentially one after the other, even if you don't notice it.

I would recommend you to give a look to the following article, it really explains how timers and asynchronous events work, it will also help you to understand the single-threaded nature of JavaScript:

  • How JavaScript Timers Work

And as an alternative you could give a look to WebWorkers, is a way to run scripts in separate background threads, but they are only supported by modern browsers.

like image 2
Christian C. Salvadó Avatar answered Oct 21 '22 02:10

Christian C. Salvadó