Async not working as expected. "Not Found" instantly.
See original GitHub issuenode.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:
- Created 4 years ago
- Comments:5 (1 by maintainers)
Top 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 >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 FreeTop 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
Top GitHub Comments
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 calledreturn next()
strictly or not.I often made mistakes like yours when I was young. Good luck~~
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.