简体   繁体   English

异步递归在Node.js中是什么意思

[英]What does asynchronously recursive mean in nodejs

In Mongoose documentation for validation they say " Validation is asynchronously recursive: when you call Model#save, sub-document validation is executed. If an error happens, your Model#save callback receives it " 在Mongoose的验证文档中,他们说:“ 验证是异步递归的:当您调用Model#save时,将执行子文档验证。如果发生错误,则Model#save回调会收到该消息

What do they mean by it? 他们是什么意思? Suppose I have 5 validations than will they be run in parallel or series? 假设我有5个验证,而不是并行或串行运行? Or is they get called in series with a callback that is received by the model save method? 还是在模型保存方法收到的回调中依次调用它们? Please help me understand this? 请帮我了解一下吗? I know its a very easy question But I could not get any help from searching and all. 我知道这是一个非常简单的问题,但是搜索和所有操作都无法获得任何帮助。 So please. 所以,请。

It means that, despite the fact that you have multiple events triggering "simultaneously" each method maintains it's own state, and it's own error handling steps separately for each request and for each step. 这意味着,尽管您有多个事件“同时”触发,每个方法仍保持其自己的状态,并且针对每个请求和每个步骤分别拥有自己的错误处理步骤。

Consider this example in which we need disk information to initialize a database query. 考虑这个示例,在该示例中,我们需要磁盘信息来初始化数据库查询。 Each successive step is an asynchronous operation, that calls a callback. 每个后续步骤都是一个异步操作,该操作称为回调。

function dataBaseQuerys(someQuerys) {
    function queryDatabase(someQuery) {

        if(someQuery === undefined) return;

        fs.readFile('someFile', function (err, data) {
            'use strict';

            var myQuery = someQuery + data;

            query(myQuery, function (err, qData) {
                doSomethingWithTheQuery(qData);
                queryDatabase(someQuerys.pop());//Recursion(behaves iteratively, but with async calls!  No for loop necessary)
            });
        });
    }

    queryDatabase(someQuerys.pop());
}

Now consider the following example in which we don't take advantage of async recursion. 现在考虑下面的示例,其中我们没有利用异步递归。

function dataBaseQuerys(someQuerys) {

    for(var i = 0; i < someQuerys.size; i++) {
        var fileData = fs.readFileSync('someFile');

        var myQuery = someQuerys[i] + fileData;

        doSomethingWithTheQuery(querySync(myQuery));//Finding a DB module that does a "querySync" is probably not possible, but assume it is.
    }
}

These two examples do the same thing. 这两个示例做同样的事情。 The iterative one is a little more difficult to get the error handling correct, and try/catch in javascript is bad because it disallows optimization. 迭代式操作很难正确处理错误,而在javascript中尝试/捕获是不好的,因为它不允许优化。 The recursive one, the syntax is a little tricky with getting the closure data for the "nextQuery" object and such, but it doesn't clog the event loop, which is priceless for performance. 递归的语法在获取“ nextQuery”对象的关闭数据等方面有点棘手,但不会阻塞事件循环,这对于性能而言是无价的。 All the developers are telling you is that they used the first example. 所有开发人员都告诉您,他们使用了第一个示例。

EDIT: I'm gonna hand hold a little bit here and answer your bullet points as they pertain to these examples: 编辑:我要在这里稍稍举一些,并回答与这些示例有关的要点:

What do they mean by it? 他们是什么意思? Honestly can't get more specific here... my example explicitly outlines the idiom they are using. 老实说,这里没有更多具体...我的示例明确概述了他们正在使用的惯用法。

Suppose I have 5 validations than will they be run in parallel or series? 假设我有5个验证,而不是并行或串行运行? -They will run in series, asynchronously. -它们将以异步方式串联运行。 This is the point of the "recursion". 这就是“递归”的重点。 Each point in code is guaranteed to run in order, but other things can happen in the background, just not things involving any of the callback sequences the queries are implementing. 代码中的每个点都保证可以按顺序运行,但是其他情况也可能在后台发生,只是不涉及查询正在实现的任何回调序列的情况。

Or is they get called in series with a callback that is received by the model save method? 还是在模型保存方法收到的回调中依次调用它们? -YES -是

Please help me understand this? 请帮我了解一下吗? - If this does not help, you need to ask a more specific question. -如果这样做没有帮助,则需要提出更具体的问题。 Also, if you don't see how the logic I laid out in my examples covers all of these questions, you should spend a little more time wrapping your head around async "event based callbacks". 另外,如果您看不到我在示例中提出的逻辑如何涵盖所有这些问题,那么您应该花更多的时间来解决异步“基于事件的回调”问题。 If you're accustomed to C-style logic flow it can be hard to wrap your head around, I've been there and wish someone at the time would have told me the same. 如果您习惯了C风格的逻辑流程,可能很难把头缠住,我去过那里,希望当时的人也能告诉我同样的事情。

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

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