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.

Koa not waiting on await

See original GitHub issue

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

github_iconTop GitHub Comments

31reactions
fedgrantcommented, May 14, 2018

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.

4reactions
singhravi1commented, Jul 14, 2020

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

Read more comments on GitHub >

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

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