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.

Async not working as expected. "Not Found" instantly.

See original GitHub issue

node.js version: 12.8.1

npm/yarn and version: 6.10.2

koa-router version: 7.4.0

koa version: 2.6.2

Code sample:

router.get('/async', async (ctx) => {
  return new Promise((resolve, reject) => {
    console.log('wait...');
    setTimeout(() => {
      ctx.ok('ok after 1s');
      console.log('resolve');
      resolve('asd');
    }, 1000);
  });
});

Expected Behavior:

the request should wait 1s before responding with either ‘ok after 1s’ or ‘asd’

Actual Behavior:

instant response with “Not Found” although timeout is started and stopped as expected. But the results from the timeout not appearing in response since response is already output and connection closed (prematurely IMHO)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
lichaozhycommented, Aug 26, 2019

I guess the cause may just be some middlewares not calling return next() correctlly. Specially, the router middleware is an async function but parent middlewares are normal sync functions. I think you should check all of your middlewares has been called return next() strictly or not.

I often made mistakes like yours when I was young. Good luck~~

0reactions
spidgornycommented, Aug 28, 2019

You are right, there was a middleware above which was not using async/await. I was trying to fix it in the wrong place. This is fixed now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Async not working as I expected - Stack Overflow
First, you're missing await in SubTask2() , so it actually runs synchronously (waits for LongRunningOperation2() to finish). Secondly, even with ...
Read more >
find* methods does not work as expected? · Issue #82 - GitHub
My guess is findBy* isn't working because the element isn't appearing asynchronously, it's already there. If the element is already in the tree ......
Read more >
Async waits in React Testing Library - Reflect.run
In the waitFor callback, we assert that the loading text is not present in the document. As you may be aware, the queryByText...
Read more >
Async Methods - Testing Library
findBy queries work when you expect an element to appear but the change to the DOM might not happen immediately. const button =...
Read more >
How to use React Testing library to wait for async elements, a ...
queryByText('HackerNews frontpage stories loading...'); // expect(loadingText).not.toBeInTheDocument(); // }); await waitForElementToBeRemoved( ...
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