Node JS Express Handling multiple requests

后端 未结 1 867
粉色の甜心
粉色の甜心 2020-12-21 10:11

What I Have: I have a nodejs express server get endpoint that in turn calls other APIs that are time consuming(say about 2 seconds). I have called this func

相关标签:
1条回答
  • 2020-12-21 10:36

    You are apparently misunderstanding how node.js, asynchronous operations and promises work. Assuming your long running asynchronous operations are all properly written with asynchronous I/O, then neither your requestAsync1(params) or requestAsync2(params) calls are blocking. That means that while you are waiting for Promise.all() to call its .then() handler to signify that both of those asynchronous operations are complete, node.js is perfectly free to run any other events or incoming requests. Promises themselves do not block, so the node.js event system is free to process other events. So, you either don't have a blocking problem at all or if you actually do, it is not caused by what you asked about here.

    To see if your code is actually blocking or not, you can temporarily add a simple timer that outputs to the console like this:

    let startTime;
    setInterval(function() {
        if (!startTime) {
            startTime = Date.now();
        }
        console.log((Date.now() - startTime) / 1000);
    }, 100)
    

    This will output a simple relative timestamp every 100ms when the event loop is not blocked. You would obviously not leave this in your code for production code, but it can be useful to show you when/if your event loop is blocked.


    I do see an odd syntax issue in the code you included in your question. This code:

    someFunctionCall(params, callback)
    {
      // do some asyncronous requests.
      Promise.all([requestAsync1(params),requestAsync2(params)]).then
      {
         // do some operations
         callback(response) // callback given with some data from the promise
      }
    
    }
    

    should be expressed like this:

    someFunctionCall(params, callback)
    {
      // do some asyncronous requests.
      Promise.all([requestAsync1(params),requestAsync2(params)]).then(function(response)
      {
         // do some operations
         callback(response) // callback given with some data from the promise
      }
    
    });
    

    But, an even better design would be to just return the promise and not switch back to a plain callback. Besides allowing the caller to use the more flexible promises scheme, you are also "eating" errors that may occur in either or your async operations. It's suggest this:

    someFunctionCall(params) {
      // do some asyncronous requests.
        return Promise.all([requestAsync1(params),requestAsync2(params)]).then(function(results) {
            // further processing of results
            // return final resolved value of the promise
            return someValue;
        });
    }
    

    Then, then caller would use this like:

    someFunctionCall(params).then(function(result) {
        // process final result here
    }).catch(function(err) {
        // handle error here
    });
    
    0 讨论(0)
提交回复
热议问题