1

Obviously, the reason here is because of the async nature of on('data'), and the combination of using yield, but how can I make this work? I tried "thunkifying" _this.req.on, but to no avail. I'm still getting the hang of generators, yield and thunks but I'm a bit stumped here.

Is there any other clever solution without requiring another module dependency?

var thunkify = require('thunkify');
var request = thunkify(require('request'));

//...

if (['GET', 'DELETE'].indexOf(_this.req.method) === -1) {
        _this.req.on('data', function(chunk) {
            options.json = chunk.toString();
        });
    }

//...

var resp = yield request(options);
_this.body = resp[0].body;
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Jan Carlo Viray
  • 11,856
  • 11
  • 42
  • 63
  • Can you please show your whole code? I think I can answer your question, but I'm not sure what you are doing there. – Bergi Jul 14 '14 at 23:07

1 Answers1

0

I was able to make this work by using co-body module. Here's an example:

var thunkify = require('thunkify');
var request = thunkify(require('request'));
var parse = require('co-body');

//...

if (['GET', 'DELETE'].indexOf(_this.req.method) === -1) {
        options.json = yield parse.json(_this.req);
    }

//...

var resp = yield request(options);
_this.body = resp[0].body;

BUT, I don't like the fact that I have to require another module every time I have to do something. Anyone got a better one?

Jan Carlo Viray
  • 11,856
  • 11
  • 42
  • 63