Comments (5)
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.
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.
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.
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.
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)
- Receive 404 with busyboy HOT 1
- Using two routers, the next bug of regular routing HOT 5
- [email protected] will cause error HOT 2
- Route followed by required verbs and handler syntax HOT 1
- .url() and other methods that take route names are awkward to use with nested routers because of name collisions HOT 2
- better to check path before register HOT 2
- Global middleware for 404 overwrites allowed methods response output HOT 2
- A catch-all 404 handler for koa? HOT 4
- [feat] ctx's key of _matchedRoute
- [fix] The directory in the API documentation does not work. HOT 1
- Support multiple hosts for routing without use of RegExp
- [feat] koa 相关库的介绍文档不够详细,如何使用 HOT 1
- [feat] add support for path-group style routing HOT 1
- [fix] Match the right controller when multiple routes matches without calling next() HOT 2
- [feat] Add request validation guard with jsonSchema
- [fix] router.use method doesn't work with RegExp
- [fix] router.use doesn't work with middleware.
- v11.0.2 is tagged as latest in npm, instead of v12.0.1 HOT 1
- [fix] ctx.request.body 不能设置类型标注
- [fix] @koa/router does not handle decoding URL with '+' character properly
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from router.