On a fresh project, two requests coming in resolve sequentially and not in parallel when waiting on a setTimeout in a service
See original GitHub issueBug Report
Current behavior
Generate fresh project
run yarn
Change the app.service.ts
file
run yarn start
Create latency using a timeout
send two almost simultaneous requests
one returned in ~6 seconds, and the other returns in ~12 seconds
repeated with yarn build
and yarn start:prod
with a similar result
Input Code
I generated a fresh Nest project (typescript) and changed the app.service.ts
file to the below and fixed the TS error in app.controller.ts
import { Injectable } from '@nestjs/common';
@Injectable()
export class AppService {
async getHello(): Promise<string> {
// await 5 seconds
await new Promise<void>((res) => {
setTimeout(() => {res()}, 5000);
});
return 'Hello World!';
}
}
Expected behavior
I would expect the calls to resolve in parallel, even on a single core given that the promises would be put off into worker threads
Possible Solution
I’m pretty stumped as to why this is happening
Environment
Nest version: 8.0.6 (happened on 7.x as well)
For Tooling issues:
- Node version: v14.15.1
- Platform: Mac 6 core
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Resolve promises one after another (i.e. in sequence)?
Because promises execute immediately, we can't just create an array of promises, they would all fire off in parallel. Instead, we need to...
Read more >beforeAll() executed in parallel with test case in case of timeout
Bug Report Test case is executed even if beforeAll did not finish. To Reproduce This test file: beforeAll(async () => new Promise((resolve, ...
Read more >Aggregate Multiple API Requests with Promise.all()
In this post we'll explore handling concurrent API requests into an aggregated response. This is used in building an aggregation service ...
Read more >Running Promises in a loop sequentially, one by one - Medium
We want to call the delay function with each value, and we expect our script to wait 1 second, then 2 seconds, then...
Read more >JavaScript Promises: race, all, allSettled, and then
In this article, we will cover three JavaScript Promises that serve complex use cases and show you how to deal with multiple promises...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’m not able to replicate this using
curl
. I can take your service code and doAnd I get a response back from the background processes after about 5 seconds.
I think that would be an issue of the active window. Browsers sometimes seem to put requests on hold momentarily if windows are switched. That’s really my only guess. If you send two requests from the browser console, they’d probably come back around the same time