r/javascript • u/Not_Edward_Bernays • Oct 29 '10
c# programmer: JavaScript is "completely single threaded and blocking" in relation to fetching from a URL.
/r/programming/comments/dxv7a/asynchrony_in_c_5_part_one/c13rj6k8
u/adelle Oct 29 '10
This:
Javascript is only non blocking in situations where you are dispatching off to the browser do to something. In that situation, the browser is spawning a XmlHttpRequest on a separate thread.
I can do the same thing in VB6 using WinHTTP. It doesn't make VB6 multithreaded. Even the callback is executed on the same thread as I started on.
However, you cannot do this in your own code. There is no way to take an arbitrary task and run it asynchronously without blocking.
This is why there is the WebWorkers spec for HTML5.
5
4
u/drowsap Oct 29 '10
Can someone explain what is going on here? You can spawn timers and XMLHTTPRequests but the execution of their callback function will be single threaded. I don't get what is going on here? And I also thought it was true that you can have up to two simultaneous http requests to the same domain only?
6
u/BusStation16 Oct 29 '10
That what makes this funny.
You can have multiple (more than 2 even) simultaneous http requests to the same domain, JS however blocks while it is executing.
7
u/wendall911 Oct 29 '10
Reminded me of this blog entry: http://ejohn.org/blog/how-javascript-timers-work/ It's a fine illustration through a feature on how JS is single threaded and blocking.
Here is a excerpt:
Since JavaScript can only ever execute one piece of code at a time (due to its single-threaded nature) each of these blocks of code are "blocking" the progress of other asynchronous events. This means that when an asynchronous event occurs (like a mouse click, a timer firing, or an XMLHttpRequest completing) it gets queued up to be executed later (how this queueing actually occurs surely varies from browser-to-browser, so consider this to be a simplification).
4
u/BusStation16 Oct 29 '10
This is an excerpt from my work-in-progress book: Secrets of the JavaScript Ninja. To be released Fall 2008.
...still waiting
3
3
u/siddboots Oct 29 '10
My first impression was to upvote this to further embarrass Not_Edward_Bernays when he eventually realises that he has missed the point. After reading his comments, however, I changed my mind.
No one needs to see shit like this.
1
u/MrBester Oct 29 '10
DAE think the crosspost is like Blazing Saddles when the cowboy fight spills over into the next soundstage where a musical is being filmed?
-2
-12
u/Not_Edward_Bernays Oct 29 '10
when I am waiting for a url to fetch, I can still interact with the browser.
so it is not really blocking. in the c# example, if he doesn't do all of his bullshit, then it really will block him and it will be obvious that it is.
9
Oct 29 '10
It depends on how Archive(FetchUrl()) in the C# example works internally.
For example, it may spawn a thread and do all the work in that thread, and return immediately (ala, how DOM operations work in a browser). This doesn't take C# 5. You could do that with C# 1.0
I don't think you understood the article at all.
3
3
0
u/greim Oct 29 '10
In both browser-based JS and node.js, IO is passed off to modules written in C or C++, meanwhile JS loops and hangs out and is free to do other stuff. Yes, this is non-blocking and asynchronous and all that good stuff.
But what I think these good C# folks are trying to say is that with C#, this capability is not implemented as separate modules which are then bolted onto the implementation and exposed through a callback-driven API, but rather are a core part of the language, and have been factored into the language design at a core level in such a way as to be somehow more convenient.
It's a C# thing, you have to just let it go man.
0
Oct 29 '10
[deleted]
6
u/wendall911 Oct 29 '10
I think /r/javascript may have been the wrong place to complain. Maybe /r/butthurt ... Even though you gave a very clear an friendly response.
4
Oct 29 '10
I think Edward_Bernays expected you all to dogpile on me, instead of agree, otherwise why would he post it here?
2
u/wendall911 Oct 29 '10
Oh, he's even trying to bury now. Nice. Wonder how he'll react to the quote from John? Clearly that guy doesn't know anything about JS. ;)
21
u/ruinercollector Oct 29 '10
You are an idiot.