Koa not waiting on await
See original GitHub issueHaving a issue where Koa is returning early and not waiting for any of my await functions to finish. Node: 9.10
//root.js
module.exports = async (ctx, next) => {
const test = new Promise(function(resolve, reject) {
setTimeout(() => {
return resolve('word');
}, 1000);
});
ctx.body = await test
}
//index.js
app.use(async ctx => {
switch(ctx.path) {
case '/':
await root(ctx);
break;
}
})
This always throws a 404. How can I get Koa to wait for the await function to finish?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:18 (4 by maintainers)
Top Results From Across the Web
Koa is not waiting for MongoDB Response and returning null ...
Try Promise.all instead new Promise router.get("/getcustomers", async (ctx) => { ctx.customers = [await remind_col.find({}).
Read more >From express to Koa. 'How using async/await push me from…
With NodeJS version 7 came the support of async/await function. Koa just plays more naturally with them. This will show how.
Read more >Understanding async function with Koa | by GoFrendi Gunawan
It just append World to ctx.body , wait for the next middleware (if any) and return true , which is equal to resolve(true)...
Read more >Egg and Koa - Eggjs.org
In async function, we can use await to wait for a promise to be resolved(or rejected, which will throw an exception), and Node.js...
Read more >Koa 故障排除 | koa-docs-Zh-CN - GitHub Pages
The code doesn't tell Koa to wait for the database to return the record. Koa sends the response after the middleware has been...
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
Ended up figuring it out. I was testing this code inside of a larger koa application. And in one of the other middleware files, I was not using
await next()
, when I updated my code everything worked properly. My bad.I spent many hours finding the issue and this made it clear ! thanks a lot @fedgrant