0

I have a helper constructor that uses a yield statement:

var co = require('co');

var Helper = co(function* () {
    var response = yield foo();
})
module.exports.Helper = Helper;

I would like to instantiate it:

var Helper = require('./helper.js').Helper;
var helper = new yield Helper();  // TypeError

But I get the following error: TypeError: You may only yield a function, promise, generator, array, or object, but the following was passed: "[object Object]".

Any ideas on how to instantiate it correctly?


Edit

Removed references to koa, because I'm using co (as pointed out by vanthome).

Felix
  • 3,783
  • 5
  • 34
  • 53
  • 1
    related if not duplicate: [Is it bad practice to have a constructor function return a Promise?](http://stackoverflow.com/q/24398699/1048572) (or: …use the `yield` keyword) – Bergi Jan 27 '15 at 21:08

1 Answers1

1

There is no point in using the new operator on a generator function here. operator.

Also your example has nothing to do with koajs, you are plainly using co here, which is a different thing. Sinve version 4 of co, the call co(...) gives you a promise that you can work with.

vanthome
  • 4,816
  • 37
  • 44
  • Ah, I seem to be muddling a few things here. Thanks for response! – Felix Jan 27 '15 at 20:39
  • 1
    I don't think `new yield …` would be syntactically wrong. You just needed to have the yield expression return a constructor function :-) – Bergi Jan 27 '15 at 21:10
  • Use this guide as introduction to Generators: http://davidwalsh.name/es6-generators It's the best I know. @Bergi: you're so right, I've updated the answer. – vanthome Jan 27 '15 at 22:10