简体   繁体   English

异步 Function 使用 node.js 递归调用

[英]Async Function Call in Recursion using node js

How to call Async function in recursion actually I have Async method - async function getSSN(req,res,next) and inside this function block I need to call this function but when I call this method by code - return await getSSN(req,res,next) this code is not working for me in node.js. Can anyone give me solution for that.?如何在递归中调用异步 function 实际上我有异步方法 - 异步 function getSSN(req,res,next)并且在这个 function 块中我需要调用这个 function 但是当我通过代码调用这个方法时 - return await getSSN(req,res,next)这段代码在 node.js 中对我不起作用。谁能给我解决方案。?

So, you can't mix async/await with plain asynchronous callbacks.因此,您不能将async/await与普通的异步回调混合使用。 They do not work properly.它们无法正常工作。 Your async function getSSN() is not properly resolving when your request.get() finishes because that's just a plain asynchronous callback which the async function knows nothing about.当您的request.get()完成时,您的async function getSSN()未正确解析,因为这只是一个普通的异步回调,而 async function 对此一无所知。

Instead, you need to use a promise-based request() option.相反,您需要使用基于承诺的request()选项。 Since, the request library is now deprecated and should not be used for new projects, I would suggest one of the alternatives that are all based on promises already.由于 request 库现在已被弃用并且不应用于新项目,因此我建议使用一种已经全部基于 promises 的替代方案 My favorite is the got() library , but you can look at the list of alternatives and pick the one that has a programming style you like.我最喜欢的是got(),但您可以查看备选方案列表并选择具有您喜欢的编程风格的一个。

In addition, your getSSN() function has a bunch of side effects (modifying higher scoped variables).此外,您的getSSN() function 有很多副作用(修改更高范围的变量)。 This is not a good way to program, particularly for asynchronous operations because the outside world generally doesn't know when things are done and when the higher scoped variables have the values in them you want them to.这不是一种好的编程方式,尤其是对于异步操作而言,因为外界通常不知道事情何时完成以及更高范围的变量何时具有您希望它们具有的值。 You should make getSSN() return a promise that resolves to its result and then have the caller use that result to modify the higher scoped variables.您应该让getSSN()返回一个解析为它的结果的 promise,然后让调用者使用该结果来修改更高范围的变量。 Again, if you showed us the rest of the coding context here, we could suggest a better overall way to do this.同样,如果您在此处向我们展示了编码上下文的 rest,我们可以建议一种更好的整体方法来执行此操作。 Please notice here that you're just not providing enough code for us to be able to show you the best way to write this code.请注意,您只是没有提供足够的代码,我们无法向您展示编写此代码的最佳方式。 That should be a general lesson for stackoverflow.这应该是 stackoverflow 的一般教训。 Err on the side of giving us more than you think and we can then often make suggestions and improvements far beyond what you even know to ask about.宁可给我们比您想象的更多的东西,然后我们通常可以提出远远超出您甚至不知道要问的建议和改进。

Here's your function with all the ill-adivsed side effects still in it (because you haven't shown us the rest of the code) using got() :这是您的 function,其中仍然存在所有不良副作用(因为您没有向我们展示代码的 rest),使用got()

async function getSSN(next) {
    const result = await got('idms.dealersocket.com/api/account/…').json();
    if (count <= totalpage) {
        const data = Object.assign({}, result.Data);
        const len = Object.keys(data).length;
        for (var i = 0; i <= len; i++) {
            ssn.push(data[i].Row.Borrower1SSN);
        }
        count++;
    }    
}

Now, getSSN() will return a promise that resolves or rejects when the.network request is finished.现在, getSSN()将返回一个 promise,它在 .network 请求完成时解析或拒绝。

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

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