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?
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:
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/