expressjs is not asynchronously processing my requests

var data = import('./blahblah/data')
app.get('/data', data.getData)
app.listen(3000)

// blahblah/data:
exports.getData = function(req, res) {
    setTimeout(function(){
        res.send('test')
    }, 10000)
};

  • Error handling in a Promise.all like flow in async/await syntax
  • JavaScript: How to download JS asynchronously?
  • how to make Nodejs call synchronous then I can use it easily?
  • What patterns are there for passing state through a chain of promises in Javascript?
  • Any way to control Javascript Async Load Order?
  • Nodejs parallel with promise
  • //code I use to test
    for(var i = 0;i<10;i++){
        $.ajax({
          url: 'http://127.0.0.1:3000/data',
          success: function(l){console.log(l);}
        });
    }
    

    If I send 10 simultaneous requests to this endpoint, then they only receive a reply one at a time 10 seconds apart, with the last request coming 110 seconds after the first request was sent. Aren’t nodejs and express supposed to let other requests process when the other requests are running asynchronous code?

  • JavaScript checking if resource is reachable with fetch
  • Mocking asynchronous service function using angular/karma/jasmine
  • Javascript: Is there any way to detect when all async scripts have loaded?
  • How do async tests work in intern?
  • Why can I not throw inside a Promise.catch handler?
  • Conditionally executing a callback within an asynchronous function call in a for loop
  • One Solution collect form web for “expressjs is not asynchronously processing my requests”

    Your code is fine, probably something is wrong with the way you’re testing it.

    Please, make sure that you’re sending your requests in parallel.

    I tested your code with loadtest utility

    loadtest -n 10 -c 10 http://localhost:3000/data
    

    and got expected results

    Target URL:          http://localhost:3000/data
    Max requests:        10
    Concurrency level:   10
    Agent:               none
    
    Completed requests:  10
    Total errors:        0
    Total time:          10.050681130000001 s
    

    Here is a code snipped I tested:

    var app = require('express')();
    app.get('/data', function(req, res) {
        setTimeout(function(){
            res.send('test')
        },10000)
    });
    app.listen(3000)
    

    Update:

    Web browsers usually limit the number of parallel requests per host. I just tested it in my Google Chrome and it limited the number of parallel requests by 6:

    enter image description here

    Here is a detailed timing for one of delayed requests:

    enter image description here

    Looks like your browser have an even harsher limits.