Why is a function and a callback non-blocking in Node.JS?

后端 未结 4 1783
你的背包
你的背包 2020-12-06 01:37

The novice understanding of Node is that if I re-write synchronous, or in-line code, to utilize functions / callbacks, I can ensure that my code is non-blocking. I\'m curio

4条回答
  •  忘掉有多难
    2020-12-06 02:19

    IMPORTANT - The db.query() does not block the stack, as it is executed elsewhere.

    Example 1 blocks because line 2 needs information from line 1 and therefore has to wait until line 1 completes. Line 3 cannot be executed before line 2 as code in processed in order of line, and therefore line 2 blocks line 3 and any further lines.

    Example 2 is non-blocking as the callback function is not called to be executed until db.query is finished, therefore it does not block doSomethingElse() from being executed.

提交回复
热议问题