简体   繁体   中英

Execution of asynchronous code in nodejs

I am relatively new to nodejs development. 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. 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 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.).

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. 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 .

Just as the MDN docs state, the function passed into new Promise() is executed synchronously.

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() :

 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:

 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
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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