question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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 issue

Bug 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:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
jmcdo29commented, Aug 24, 2021

I’m not able to replicate this using curl. I can take your service code and do

sendTwoRequests() {
curl http://localhost:3000 &
curl http://localhost:3000 &
}
sendTwoRequests

And I get a response back from the background processes after about 5 seconds.

1reaction
jmcdo29commented, Aug 25, 2021

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found