Giter Club home page Giter Club logo

Comments (5)

lichaozhy avatar lichaozhy commented on August 15, 2024 2

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

from router.

jamesaspence avatar jamesaspence commented on August 15, 2024

Interesting that you have an async function and are not using await. But I'd remove the async as you're not using it. You can change your code to this:

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

from router.

jamesaspence avatar jamesaspence commented on August 15, 2024

To clarify - this will run your code. If you want to return ok after 1s ctx.ok will not do it - I don't know where you got that from but to return a body you can use ctx.body.

from router.

fl0w avatar fl0w commented on August 15, 2024

So, I'm not sure what ctx.ok is, I'm assuming that's something custom you have (because it aint part of Koa).

Anyway, this works fine IMO:

// index.js
const Koa = require('koa')
const Router = require('@koa/router')

const app = new Koa()
const router = new Router()

router.get('/async', (ctx) => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
        ctx.body = 'ok after 1s' // changed from unknown `ctx.ok`
        console.log('resolving')
        resolve()
    }, 1000)
  })
})

app
  .use(router.routes())
  .listen(8080)
$ node index.js &
$ time curl localhost:8080/async
resolving # console.log from within setTimeout
ok after 1s # http response body
real	0m1.020s
user	0m0.005s
sys	0m0.005s

So, either the implementation of ctx.ok is broken, or you have a try-catch somewhere in the middleware chain which is swallowing the error thrown by mistake. ctx.ok() should throw a TypeError because it's not a function by the way - so if you simply mistook Koa api with something else you should see it, or you're catching that as well somewhere.

from router.

spidgorny avatar spidgorny commented on August 15, 2024

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.

from router.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.