简体   繁体   English

在Node.js中执行异步代码

[英]Execution of asynchronous code in nodejs

I am relatively new to nodejs development. 我对nodejs开发比较nodejs I need to execute a function asynchronously doing some unimportant stuff, that's the reason I want to do it asynchronously like recording the no. 我需要异步执行一些不重要的事情的函数,这就是我想要像记录no那样异步执行的原因。 of calls this method has received. 此方法已收到的呼叫数量。 The recording part is not super critical to me and does not want it to hinder or slow down the main flow in anyway. 录音部分对我来说不是很关键,也不希望它阻碍或减慢主流程。

I have considered using return Promise like this: 我已经考虑过使用return Promise这样的方式:

return new Promise( /* executor */ function(resolve, reject) { ... } );

But observed that the executor starts executing immediately, as mentioned in the Mozilla docs ( Not sure about it though.). 但是观察到执行器立即开始执行,如Mozilla文档中所述 (尽管不确定)。

I am not interested in this behavior because then some computations of the executor function will be running before my normal flow(the caller function) could continue. 我对此行为不感兴趣,因为在正常流程(调用者函数)可以继续之前,将运行executor函数的某些计算。 I know I should not keep the sync portion computationally intensive, and I have not kept it intensive. 我知道我不应该保持同步部分的计算强度,也没有保持它的强度。

Pseudo code for the my flow looks something like this: 我的流程的伪代码如下所示:

export let mainFunc = (req: Request, res: Response) => {
     // logic for handling the request is here
     // need to record analytic information here by calling
     recordInformation(req);
}


function recordInformation(req){
    return new Promise(function(resolve, reject) {
        //some synchronous code followed by asynchronous code
    });
}

Just that I am looking out for a way such that the calling function mainFunc never waits for even a single computation after calling recordInformation . 只是我正在寻找一种方法,使得调用函数mainFunc在调用recordInformation之后甚至不再等待单个计算。

Just as the MDN docs state, the function passed into new Promise() is executed synchronously. 就像MDN文档所声明的那样,传递给new Promise()的函数是同步执行的。

Example: 例:

 function temp() { return new Promise(function(resolve, reject) { console.log('This runs right away'); }); } temp(); temp(); temp(); console.log('This runs last'); 

If you want to run some code asynchronously, pass it into a .then() : 如果要异步运行某些代码,请将其传递给.then()

 function temp() { return Promise.resolve().then(function () { console.log('This runs later'); }); } temp(); temp(); temp(); console.log('This runs right away'); 

Bear in mind that if all you want to do is have some code run after the current execution stack has finished, then a simple setTimeout could probably be just as good: 请记住,如果您只想在当前执行堆栈完成后运行一些代码,那么简单的setTimeout可能就一样好:

 function temp() { setTimeout(function () { console.log('This runs later'); }, 1); } temp(); temp(); temp(); console.log('This runs right away'); 

From your pesudo-code; 从您的伪代码; you can respond to the request, then call you other function after that. 您可以响应该请求,然后再调用其他函数。 This would mean that all of your request handling including the response is executed before your other function: 这意味着您的所有请求处理(包括响应)都在其他功能之前执行:

export let mainFunc = (req: Request, res: Response) => {
     // logic for handling the request is here
     res.end('Hello World');
     // response is now sent, feel free to do anything else at your leisure..
     // need to record analytic information here by calling
     recordInformation(req);
}


function recordInformation(req){
    return new Promise(function(resolve, reject) {
        //some synchronous code followed by asynchronous code
    });
}

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

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