Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Requests to my express app gets handled in a sync way (instead of async) #5869

Closed
VenkatSaiVikram opened this issue Aug 28, 2024 · 2 comments
Labels

Comments

@VenkatSaiVikram
Copy link

const express = require("express");

const app = express();

app.all('*', async (req, res) => {
    const reqStartTime = Date.now();
    const random = Math.floor(Math.random() * 10);
    console.log("req received ", req.path, " startTime ", reqStartTime, " randomNum ", random);
    if (random > 5) {
        const promise = new Promise((resolve) => {
            setTimeout(resolve, random * 1000);
        });
        await promise;
    }
    console.log("completed ", req.path, " ", Date.now() - reqStartTime);
    res.send('Hiiiiiiiii');
});

app.listen(3001, () => {
    console.log("Server is running on port 3001");
});

This is my basic express app. When I start the server and hit localhost:3001 in my browser for the first time(lets say 8 seconds) it waits for 8 seconds before sending the response.. but if i hit the same request in another tab (irrespective of the random timeout generated), my second request waits until the first one completes and then gets resolved..

Any issues with my code? or am I missing something?
node version - 18.14.2 & npm version - 9.5.0

@TheDecipherer
Copy link

TheDecipherer commented Aug 28, 2024

const express = require('express')

const app = express()

app.use(express.json())

app.post('/get', async (req, res) => {
    console.log(req.body)
    const promise = new Promise(resolve => {
        setTimeout(resolve, 8000)
    })

    await promise
    
    console.log(req.body)
    res.send(req.body)
})

app.listen(8000, () => {
    console.log("App running in port 8000")
})

This was my sample code. I kept the timeout fixed and the request did not wait. The same should be the case for random timeouts too. Please recheck.

@VenkatSaiVikram
Copy link
Author

VenkatSaiVikram commented Aug 28, 2024

image
This is my modified code and console log order.. first request enters and waits for 8 seconds and seconds request waits for 16 seconds (8 for first and after first completes 8 for second)

I tried the same code with another machine.. its working fine.. what might be the issue with my machine??

image
console log with time for ref.

@expressjs expressjs locked and limited conversation to collaborators Aug 29, 2024
@wesleytodd wesleytodd converted this issue into discussion #5875 Aug 29, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
Projects
None yet
Development

No branches or pull requests

2 participants