0

I'm from a C background and find the asynchronicity of javascript very cool. I don't know however how things are asynchronous. Is it that every function-call is practically a new thread?

3
  • That's how I've been programming in it. Could you elaborate? Commented Nov 20, 2013 at 1:17
  • You can find a very good insight here. Commented Nov 20, 2013 at 1:17
  • You will probably find this answer helpful about threading, event queue and asynchronous ajax. Commented Nov 20, 2013 at 1:32

1 Answer 1

5

No, it's not a new thread: it's running an event loop.

Examples of systems in C that work the same way:

  • select-based polling where you stay on one thread, handle the result of select, then call select again to get the next thing to work on
  • Classic Win32 programming, where you send messages to the event queue. The core of the program is "Dequeue message. Dispatch message. Repeat until quit message received"
  • Just about every other GUI programming environment ever built :-)

While you could think of it as a thread for a first approximation, it isn't really. Threads run in parallel, events are run serially. You never have to worry about concurrent access to data, but you do have to worry about starving the event loop (not returning to it often enough).

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

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.