0

Since from co@4.0+ we can use below statement

var fn = co.wrap(fn*)

to convert a generator into a regular function that returns a Promise.

Then I face a problem

a.js

var F = function *(a,b,c){
    this.a = yield this.getA(a);
    this.b = yield this.getB(b);
    this.c = yield this.getC(c);
}

F.prototype.getA = function * (a){
    //........
}
F.prototype.getB = function * (b){
    //........
}
F.prototype.getC = function * (c){
    //........
}
exports.F = F;

how to create a instance in b.js by co .


@Bergi said it`s a bad practice

then I want to ask anthor question

function* F(){
  yield this.x = 2;
  yield this.y = 3;
}
var obj = {};
var f = F.bind(obj)();

f.next();
f.next();
f.next();

console.log(obj);
// { x: 2, y: 3 }

is it a bad practice ?

pigcan
  • 93
  • 1
  • 1
  • 6
  • 1
    Generators are no constructors, and `wrap` doesn't wrap constructors. Also see [Is it bad practice to have a constructor function return a Promise?](http://stackoverflow.com/q/24398699/1048572) – Bergi Jul 29 '15 at 13:54
  • Thanks for your reply . I just re-edited my question . So what`s your opinion? – pigcan Jul 29 '15 at 14:16
  • I don't know what your second script is supposed to do, so I can't say whether it's a bad practise and whether there's a better solution to the problem. It looks a bit overcomplicated, though. – Bergi Jul 29 '15 at 14:23
  • The second script is supposed to create a instance of `F` . Because of `F` is a generator so I can not use `new` to create a instance. – pigcan Jul 29 '15 at 14:29
  • But why are you `yield`ing inside of `F` at all? – Bergi Jul 29 '15 at 14:31
  • Good question - -! `Constuctor` should pure – pigcan Jul 29 '15 at 14:44
  • `var f = F.bind(obj)();` is just `var f = F.call(obj);` – loganfsmyth Jul 29 '15 at 18:49

0 Answers0