Mercury
Mercury

Reputation: 7988

NodeJS async library will not send requests asynchronsly

Question about asynchronsousity

I've wrote 2 node express servers both running on localhost.

Server1 has a simple express REST API that receives GET requests from browser, this API will trigger a GET request to Server2, while the request (sent from Server1) is wrapped within a NodeJS async library call. Server2 will respond for each request after 10 seconds (using the good old node's setTimeout).

My thinking was that - If 2 requests sent from Server1 to Server2 (one second after the other) what would happend is:

  1. Server1 will send the first request to Server2 and will not wait for response, making the event loop available to listen to more incoming requests.

  2. After 1 second 2nd request comes in and Server1 will shoot out this one as well to Server2.

  3. Server2 will count 10 seconds for each incoming request, which eventually will respond also with ~1 second delay between the responses to Server1.

  4. Server1 will respond eventually to both request after ~11 seconds (responses to browser).

BUT NOT !!!

What I get is:

The response to browser for the 1st request is received after 10 seconds.
The response to browser for the 2nd request is received after another 10 seconds counted from the first response (making it ~20 seconds in total) as if no async mechanism is working at all.

(And by the way I tried to wrap the request that Server1 sends with async.asyncify(...), async.series(...), async.parallel(...) - 2nd request always comes back after ~20 seconds).

Why?

My severs code:

Server 1: gets both requests to localhost:9999/work1

/* ############################################
                SERVER 1
############################################ */ 
const express = require('express');
const app = express();
const async = require('async');
const request = require('request');

var reqId = 1;

function requestWork(cb) {    
    console.log("Starting request: " + reqId);
    request.get('http://localhost:8888/work2').on('response', cb);
    console.log("After request: " + reqId);
}

app.get('/work1', (req, res) => {

    reqId++;
    async.setImmediate(requestWork, function() {
        console.log("Request done!");
        res.send('Easy request done: ' + reqId);
    });
});

var port = 9999;
app.listen(port, () => console.log('Server 1, listening on port ' + port)); 

Server 2:

/* ############################################
                SERVER 2
############################################ */ 
const express = require('express');
const app = express();
const async = require('async');

var reqId = 100;

app.get('/work2', (req, res) => {

    console.log("Got work to do: " + reqId);
    setTimeout(() => {
        reqId++;    
        res.send('Big work done: ' + reqId) ;    
    }, 10000);
});

var port = 8888;
app.listen(port, () => console.log('Server 2, listening on port '+ port));

Upvotes: 0

Views: 51

Answers (1)

Mykola Borysyuk
Mykola Borysyuk

Reputation: 3411

it's not express or async problem. It's browser problem.

If you try same code but run parallel requests in different browsers you will get what you expect.

For google chrome more details can be found here.

Chrome stalls when making multiple requests to same resource?

Hope this helps.

Upvotes: 1

Related Questions