My class looks like this:
class Test {
constructor() {
}
*test() {
console.log('test');
let result = yield this.something();
return result;
}
something() {
console.log('something');
return new Promise((resolve, reject) => {
resolve(2);
});
}
}
But when I create an object from Test and call the test() method, I don't get the expected result ...
let test = new Test();
console.log(test.test()); // {}
Thought it would return 2.
Logs aren't shown as well.
What am I doing wrong here?
It works properly. You need to call next() on returned value by test method.
let test = new Test();
console.log(test.test().next());
Output
test
something
{ value: Promise { 2 }, done: false }
By calling test.test() you are creating new generator instance. Then you should call next() function on created instance to make generator yield value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With