“atomic” operation desturbed by asynchronous ajax callbacks

前端 未结 2 1086
独厮守ぢ
独厮守ぢ 2021-01-07 08:17

I know, using the words JavaScript and \'atomic\'-anything in the same sentence is kinda strange, since JavaScript is prised to be asynchronous and therefor not very atomic.

相关标签:
2条回答
  • 2021-01-07 08:24

    JavaScript is inherently single-threaded. This means that if AJAX response arrives or timeout/interval should be triggered but other code is running, the response callback/timeout will wait.

    This was one of the primary reasons why JavaScript was chosen for node.js.

    See also:

    • Are there any atomic javascript operations to deal with Ajax's asynchronous nature?
    0 讨论(0)
  • 2021-01-07 08:42

    Javascript is entirely single-threaded.

    Async callbacks can only run when no other code is running using a message queue, like Windows messages.
    All code runs on the UI thread and your code can never be interrupted in the middle (except by the Unresponsive Script warning)

    Note that Javascript does support true multi-threading using HTML5 Web Workers.

    0 讨论(0)
提交回复
热议问题