-
Notifications
You must be signed in to change notification settings - Fork 408
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
router does not await async functions #358
Comments
Here's a test case I created on my machine with const Koa = require('koa');
const Router = require('koa-router');
const app = new Koa();
const router = new Router();
router.get('/relay', async (ctx) => {
const token = await relay();
ctx.status = 200;
ctx.body = token;
});
async function relay() {
return await timeout(5000);
}
async function timeout(delay) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve("random");
}, delay);
});
};
app.use(router.routes());
app.listen(3001); And it seems to work fine:
Is this a representative sample of the code where you're having the issue? |
Yes. It does seem to be representative of my code. And it is working on my machine as well. Let me take another look at the code. I must be doing something wrong with the middlewares. Thanks for your time and help! |
How did you solve the problem? |
@mtkage I meet the problem too, could you tell me how you fixed it? Thanks a lot. |
@crcrcry @ccsang The problem usually lies in the code that you must be calling. When using async await, you need to make sure that the entire call stack uses the same paradigm. If any of the functions in the stack do not await an async function, the desired result is not seen. Check if you are using some external libraries that might be triggering functions asynchronously. |
@mtkage I examined my middlewares, finally found out the mistake I made as you said, thanks. |
I also encountered a similar problem. I got a 404 error as long as I called an asynchronous function in the route. But if I don't call any asynchronous function, there's no problem // this is wrong
app.use(function (ctx, next) {
ctx.set("Access-Control-Allow-Origin", "*");
next();
});
// this is right
app.use(async function (ctx, next) {
ctx.set("Access-Control-Allow-Origin", "*");
await next();
}); |
@mtkage I meet the problem too,thank you |
This is such a weird problem because the workaround that @wzdxy posted doesn't seem like it should even be related. But it does fix it. |
@rosshadden you need to either |
Haha I just had the realization that it actually makes perfect sense and came back here to update accordingly. But you beat me to it. |
You must |
Hi
I am using the following versions
nodejs v7.7.1
+-- koa@2.2.0
+-- koa-router@7.2.0
My problem is that the router returns without awaiting for the async function to resolve. The sample code is below:
I can see from my logs that the route
/relay
returns immediately with a404
. The timeout function is called asynchronously and when it tries to set the ctx status the following error is thrown.AssertionError: headers have already been sent
There does not seem to be a clear way to achieve what I want. I simply want to await an asynchronous function and return once it is resolved. How can I do this?
The text was updated successfully, but these errors were encountered: