admin管理员组文章数量:1321258
I'm trying out some of the harmony features in node 0.12, in particular trying out the new generators feature. I'm doing this with co (v4), bluebird and express (v4), something like this:
// ...
var fs = bluebird.promisifyAll(require('fs'));
// ...
app.post('/test', co.wrap(function* (req, res, next) {
var contents = yield fs.readFileAsync('/etc/hosts', 'utf8');
return res.send(contents);
}));
// ...
According to its documentation, co.wrap returns a normal function that returns a promise from the given generator function.
This is working fine so far, but what I'm not sure is if a) I'm leaking memory by not 'waiting' for the returned promise's result and b) If I might lose an exception thrown in my generator function, or one of the modules used by it.
Is this a good approach? Do you see anything wrong with it?.
I'm trying out some of the harmony features in node 0.12, in particular trying out the new generators feature. I'm doing this with co (v4), bluebird and express (v4), something like this:
// ...
var fs = bluebird.promisifyAll(require('fs'));
// ...
app.post('/test', co.wrap(function* (req, res, next) {
var contents = yield fs.readFileAsync('/etc/hosts', 'utf8');
return res.send(contents);
}));
// ...
According to its documentation, co.wrap returns a normal function that returns a promise from the given generator function.
This is working fine so far, but what I'm not sure is if a) I'm leaking memory by not 'waiting' for the returned promise's result and b) If I might lose an exception thrown in my generator function, or one of the modules used by it.
Is this a good approach? Do you see anything wrong with it?.
Share Improve this question asked Mar 21, 2015 at 18:14 MattMatt 1,3434 gold badges17 silver badges27 bronze badges 3-
Does
app.post
expect to await the asynchronous result of your callback somehow? – Bergi Commented Mar 21, 2015 at 20:18 - app.post is express' app.post. So, I suppose it doesn't (?) – Matt Commented Mar 21, 2015 at 21:28
-
2
It's pointless to use
co
if you're using bluebird since bluebird ships withPromise.coroutine
which is a more powerful and robust version ofco
anyway. – Benjamin Gruenbaum Commented Mar 22, 2015 at 9:31
1 Answer
Reset to default 10The problem with your approach is that if your generator function will throw some exception, it will not be passed to next middleware. So you will lose it. You can use bluebird's Promise.coroutine
function to implement your own simple co
wrapper, which will be working well in express:
// module: ../helpers/co.js
var Promise = require('bluebird');
var co = Promise.coroutine;
module.exports = function(gen) {
var coGen = co(gen);
function handle_error(err, req, res, next) {
return coGen.apply(this, arguments).catch(next);
}
function handle_request(req, res, next) {
return coGen.apply(this, arguments).catch(next);
}
return gen.length > 3 ? handle_error : handle_request;
};
UPD: I have changed the realization a little. Now it takes into account the number or arguments passed into the generator: if > 3 then error handler will be used, otherwise - request handler. It's important for express (look in the source code here and here)
Now you can use it in your code:
// module: your/router.js
// ...
var co = require('../helpers/co');
var fs = bluebird.promisifyAll(require('fs'));
// ...
app.post('/test', co(function* (req, res, next) {
var contents = yield fs.readFileAsync('/etc/hosts', 'utf8');
return res.send(contents);
}));
// ...
UPD This is solution for express with version <= 4.x. Most likely express 5.x will support promises, so you'll can use just bluebird's Promis.coroutine
without any fancy wrappers:
// module: your/router.js
// ...
var fs = bluebird.promisifyAll(require('fs'));
var co = bluebird.coroutine;
// ...
app.post('/test', co(function*(req, res, next) {
var contents = yield fs.readFileAsync('/etc/hosts', 'utf8');
return res.send(contents);
}));
// ...
本文标签: javascriptGenerator functions in express with bluebird and coStack Overflow
版权声明:本文标题:javascript - Generator functions in express with bluebird and co - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742095635a2420530.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论