Search code examples
node.jsconcurrencysettimeout

Concurrency in node js express app for get request with setTimeout


Console log Image

const express = require('express');
const app = express();
const port = 4444;

app.get('/', async (req, res) => {
  console.log('got request');

  await new Promise(resolve => setTimeout(resolve, 10000));

  console.log('done');
  res.send('Hello World!');
});

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`);
});

If I hit get request http://localhost:4444 three times concurrently then it is returning logs as below

got request
done
got request
done
got request
done

Shouldn't it return the output in the below way because of nodes event loop and callback queues which are external to the process thread? (Maybe I am wrong, but need some understanding on Nodes internals) and external apis in node please find the attached image Javascript Run time environment

got request
got request
got request
done
done
done

Solution

  • Thanks to https://stackoverflow.com/users/5330340/phani-kumar

    I got the reason why it is blocking. I was testing this in chrome. I am making get requests from chrome browser and when I tried the same in firefox it is working as expected.

    Reason is because of this

    Chrome locks the cache and waits to see the result of one request before requesting the same resource again.

    Chrome stalls when making multiple requests to same resource?