1

For example, lets say I make a function called "foobar", and inside foobar are calls to asynchronous functions. For example, it might look like this:

function foobar() {
    // asynchronous function here.
    // asynchronous function here.
}

Now, if I call foobar() five times like this:

foobar();
foobar();
foobar();
foobar();
foobar();

Will it only fire two asynchronous functions at a time?

5 Answers 5

2

No, it will fire all 10. It will fire the first two (asynchronously), then the single Javascript thread will return from the first call and enter the second one, call two more etc. Until all 10 have been called. Example:

var i = 0;
function foobar(){
    // Execute functions asynchronously by using setTimeout
    setTimeout(function(){ alert(++i); }, 0);
    setTimeout(function(){ alert(--i); }, 0);
}

foobar();
foobar();
foobar();
foobar();
foobar();
alert('This will ALWAYS alert first');

The last alert will always alert first since Javascript is single threaded, after that the other alerts will occur in any order depending on scheduling. You may see any number between -5 and 5 alerted, but the last alert will always be 0.

http://jsfiddle.net/Paulpro/uJd44/

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

5 Comments

Interesting! How come there is a delay between the first alert and all the rest?
And also, you can make any function asynchronous by putting it in setTimeout like that?
@trusktr It has to do with the blocking nature of alert. I'm not sure exactly why it takes a moment for the browser to realize it's nno longer blocked and can switch threads, but it seems to. If you change the alerts to console.log() you won't see that happen (But the execution will be so fast that the threads will almost certainly be scheduled in the order they occur so you will see 1 0 1 0 1 0 1 0 as the output.
@trusktr Yes you can make them asynchronous using setTimeout like that.
yes, you can but note that the function within setTimeout will be fired from the global scope (i.e. "this" will refer to the window) unless you do func.bind(someContext)(); where "this" will refer to someContext within function func. See for more information "the this problem" at developer.mozilla.org/en/DOM/window.setTimeout
1

The main characteristics of an asynchronous function are that it returns immediately, performs its work later, then notifies the caller that its work is done, usually through a callback.

Therefore, in your example, five calls to foobar() will result in ten asynchronous functions being fired in total, since all of them will immediately return to their caller.

Comments

0

I think every time foobar() called all the asynchronous functions will create a new copy in memory

Comments

0

The total of calls to the async function inside foobar will be 10 (5*2).

The functions inside foobar are asynchronous, so foobar will end while the other functions are still busy. Then, the next foobar is called, firing another two asynchronous functions, etc etc.

Of course, you could build a throttler to limit the amount of calls when you fire foobar quickly after each other...

2 Comments

Can you describe this throttler you speak of?
You can use an existing one here benalman.com/projects/jquery-throttle-debounce-plugin (also works without JQuery) or read about it here remysharp.com/2010/07/21/throttling-function-calls but in general a throttler uses a setTimeout() function to ensure that any given function is only executed once every so many milliseconds.
0
        foobar();  // It will invoke two asynchronous function and even if they are not   
                 //completely executed control will got to second/next foobar() method invocation
        foobar();  // Irrespective of whether first two asynchronous function have  
                  //completed or not this method will invoke two asynchronous functions  
                 // again. and next //foobar() method call will be executed

        foobar(); // Same continues
        foobar();
        foobar();

Consider if none of asynchronous method have completed execution even after invocation of last foobar() method so there will be ten asynchronous methods executing.

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.