Skip to content
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

Error when a function instead of a generator is passed into use #120

Closed
xixixao opened this issue Dec 19, 2013 · 3 comments
Closed

Error when a function instead of a generator is passed into use #120

xixixao opened this issue Dec 19, 2013 · 3 comments

Comments

@xixixao
Copy link

xixixao commented Dec 19, 2013

Given

var koa = require('koa');
var app = koa();

app.use(function () {
  this.body = 'Hello World';
});

app.listen(3000);

(Note missing asterisk after function keyword) errors out with rather unhelpful:

Error: yield a function, promise, generator, array, or object
    at next (/koatest/node_modules/koa/node_modules/co/index.js:84:10)
    at Object.<anonymous> (/koatest/node_modules/koa/node_modules/co/index.js:91:5)
    at next (/koatest/node_modules/koa/node_modules/co/index.js:74:19)
    at /koatest/node_modules/koa/node_modules/co/index.js:91:5
    at Server.<anonymous> (/koatest/node_modules/koa/lib/application.js:102:22)
    at Server.EventEmitter.emit (events.js:101:17)
    at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:505:12)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:111:23)
    at Socket.socketOnData (_http_server.js:358:22)
    at Socket.EventEmitter.emit (events.js:98:17)

A side-question: When there is no yield in the middleware, should koa, and similar frameworks, require a generator?

@xixixao
Copy link
Author

xixixao commented Dec 19, 2013

Also the client gets Internal Se (sic).

@tj tj closed this as completed in 70971dc Dec 19, 2013
@lbeschastny
Copy link

But why ordinary functions are not allowed in koa?

There is no real difference between an ordinary function and non-yielding generator function in this context.

@jonathanong
Copy link
Member

it's mostly so that developers don't get confused when they put a regular function instead of a generator function, especially when generators were new.

this restriction will eventually be removed, but for now, you can try doing app.experimental=true, which removes this restriction. #415

@koajs koajs locked and limited conversation to collaborators Mar 20, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants