r/javascript • • 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/c13rj6k
0 Upvotes

26 comments sorted by

21

u/ruinercollector Oct 29 '10

You are an idiot.

-12

u/Not_Edward_Bernays Oct 29 '10

no, I am still fucking right. the browser does not block while downloading data for AJAX. that's what I said, that's what you all have confirmed. what they are trying to achieve in that C# example is something javascript in the browser does without any weird code. so it is exactly as I characterized it, especially in this example, they are trying to catch up with javascript.

I never said the UI code runs on multiple threads.

if I could have less respect or more animosity towards you pieces of shit I would.

5

u/darrenkopp Oct 29 '10

I take it you haven't heard of BeginInvoke and EndInvoke?

Action a = new Action(() => {
    // some blocking operation
});

// run async, invokes callback when done
a.BeginInvoke(cb => a.EndInvoke(cb), null);

This is no different than the javascript example. You start something executing, and when it's done, it calls a callback. In javascript, the callback is the onreadystatechange callback. They are the exact same in principal, just different in execution.

Also, XmlHttpRequest does not mean asynchronous, it just is by default. you can have a blocking XHR request.

-1

u/Not_Edward_Bernays Oct 29 '10

I did a lot of async programming with begininvoke and such 3 or so years back

9

u/awgy Oct 29 '10

if I could have less respect or more animosity towards you pieces of shit I would.

I believe the feeling's mutual.

8

u/ruinercollector Oct 29 '10

what they are trying to achieve in that C# example is something javascript in the browser does without any weird code

Not at all. C# has supported the handling asynchronous calls by providing callbacks since 1.0, and it's supported doing so through closures since 2.0.

What is outlined in this example for 5.0 is not at all possible in javascript. You completely missed the point of the video.

7

u/franksvalli Oct 29 '10

Man, you have to realize after a while that even though you may have some valid points (XHR can be either asynchronous or synchronous, and Web Workers run on separate threads, etc), NO ONE is going to want to talk to you or side with you with when you have such a piss poor attitude. Think of a crazy man yelling on the street corner, repeating "THE EARTH IS ROUND, MOTHERFUCKERS!". It doesn't matter how correct the person is.. if the message is being delivered in such a crazy way, no one is going to want to listen or interact with you.

This is just how things are, and eventually you're going to have to realize, if even to keep a stable job, that you have to at least APPEAR to be a normal person.

(as a side note, there's definitely a bad side to this - people dressed up with a confident air are more likely to be believed, regardless of the content of their message. That's why there's so many cocksure people who rise so fast in the business world.)

3

u/octatone Oct 29 '10

LOL WHY YOU MAD THO

8

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

u/[deleted] Oct 29 '10

The funny thing is how angry Not_Edward_Bernays is about this.

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
  1. That what makes this funny.

  2. 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

u/jimrhoskins Oct 29 '10

http://www.manning.com/resig/

You can read (most of) it now.

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

u/quantumtom Oct 29 '10

This is rad. Maximum upboat.

-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

u/[deleted] 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

u/octatone Oct 29 '10

looks like your shift key is blocked

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

u/[deleted] 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

u/[deleted] 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. ;)