简体   繁体   English

节点JS代码执行

[英]Node JS Code Execution

I'm getting slightly annoyed now after reading many different articles on how to write proper code in node js. 在阅读了许多有关如何在Node js中编写正确的代码的文章之后,我现在有点恼火。

I would just like some clarification on whether I'm correct or not with each of these statements: 我只想澄清一下这些说法是否正确:

  • Code is executed synchronously 代码同步执行
  • A for loop or while loop etc. will be executed asynchronously for循环或while循环等将异步执行
  • By doing the code below isn't proper asynchronous: 通过执行下面的代码是不正确的异步:

L 大号

function doSomething(callback) {
    // doing some code

    // much much code

    callback();
}

The reason that people are saying that this won't work properly is that code is executed asynchronously, so the callback won't be fired off at the end of the code it will all be executed at once. 人们之所以说这不能正常工作,是因为代码是异步执行的,因此不会在代码的末尾触发回调,而是会立即执行所有回调。

So for example if you were filling some object by doing some stuff and you wanted to post that full object back through the callback it doesn't work because it will all be executed at the same time. 因此,例如,如果您通过做一些事情来填充某个对象,并且想通过回调将完整的对象发布回去,那么它将不起作用,因为它将全部同时执行。

No, for and while loops are synchronous in Node.js. 不, forwhile循环在Node.js中是同步的。

Whether your doSomething function example will work or not all depends on whether you're making any calls to asynchronous functions before calling callback() . 您的doSomething函数示例是否将全部正常运行,取决于您是否在调用callback()之前对异步函数进行了任何调用。 If you're making asynchronous calls, then you'd need to postpone calling callback() until those asynchronous calls have completed. 如果要进行异步调用,则需要推迟调用callback()直到这些异步调用完成为止。 If you're only making synchronous calls, then you don't need to use a callback and your function can synchronously return the result. 如果仅进行同步调用,则无需使用回调,并且函数可以同步返回结果。

Code in node.js isn't standardly asynchronous. node.js中的代码通常不是异步的。 Calls that you place one after another will still be executed one after another. 您一个接一个地放置的调用仍将一个接一个地执行。 A for or while loop will still block the main code in this way. for或while循环仍将以这种方式阻塞主代码。

However, you can pass around functions which will be executed later, through process.nextTick(callback). 但是,您可以通过process.nextTick(callback)传递稍后将要执行的功能。 This method will add a function to the event queue, which node.js takes care of internally. 此方法将向事件队列添加一个函数,node.js在内部进行处理。 This will only be executed after any other blocking code has been extecuted. 只有在检测到任何其他阻止代码之后才能执行此操作。 An example: 一个例子:

function doWork(callback) {
    //Do some work here, for instance a for loop that might take a while

    callback(workReslt);
}

function workDone(workResult) {
    //Process the result here
}

function startWork() {
    process.nextTick(doWork(workDone));
}

This will execute doWork() when the server passes through the event loop again. 当服务器再次通过事件循环时,它将执行doWork()。 This way, the startWork() function will not block the main code. 这样,startWork()函数将不会阻塞主代码。

However, most modules in node.js already implement this. 但是,node.js中的大多数模块已经实现了此功能。 Database and file access is usually non-blocking, it will simply fire a callback when it is done with it's work. 数据库和文件访问通常是非阻塞的,在工作完成后,它将仅触发回调。

If you really have to perform heavy async calculations (or perhaps some IO that doesn't already have a module for it), you can look into the async module, which has a nice tutorial here: http://justinklemm.com/node-js-async-tutorial/ 如果您确实必须执行繁重的异步计算(或者也许某些尚未具有模块的IO),则可以查看异步模块,该模块在此处提供了不错的教程: http : //justinklemm.com/node -js-async-tutorial /

As for coding conventions, think of it this way. 至于编码约定,请这样考虑。 If you know the action is going to take time, you should make it asynchronous (either through the method I mentioned first or the async module). 如果您知道该操作需要花费时间,则应使其异步(通过我首先提到的方法或异步模块)。 Actions like these are usually related to I/O, which usually also means there is already an asynchronous module for it. 此类操作通常与I / O有关,这通常也意味着已经有一个异步模块。 However, most cases I have come across up to now, have never required this. 但是,到目前为止,我遇到的大多数情况都不需要这样做。

Sources: http://howtonode.org/understanding-process-next-tick 资料来源: http : //howtonode.org/understanding-process-next-tick

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM