sigflup
sigflup

Reputation: 305

Is every function call in javascript practically a new thread?

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?

Upvotes: 0

Views: 585

Answers (1)

Chris Tavares
Chris Tavares

Reputation: 30391

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

Upvotes: 5

Related Questions