Look at this classes, Base and Derived, it's just simple classes that has "name" as property:
class Base {
constructor(name) {
this.name = name;
}
printName() {
console.log("Base: " + this.name);
}
}
class Derieved extends Base {
constructor(name) {
super(name);
}
// Override
printName() {
// IIFE.
(function() {
super.printName(); // Can't use super here
})();
console.log("Derived: " + this.name);
}
}
var obj = new Derieved('John');
obj.printName();
I want to invoke Base::printName from Derieved::printName. But some reason, I have to invoke inside of internal function of Derieved::printName.
But run above code, it fails with:
SyntaxError: 'super' keyword unexpected here
If I saved the reference of parent's method to variable, looks like it can called but can't access any properties, it saids undefined.
TypeError: Cannot read property 'name' of undefined
I just wrote that internal function is just normal function, but actually it's generator function, so I can't use arrow function:
get(match: T, options: IQueryOptions|void): Promise<Array<Object>|Error> {
const superGet = super.get;
return new Promise((resolve, reject) => {
co(function*() {
try {
// I need to invoke parent's get method here!!
const accounts = yield superGet(match, options);
... // do something with accounts
resolve(accounts);
}
catch(err) {
...
}
});
});
}
Is there a way to to this? Why I can't save the reference of parent's method into variable?
supercan be accessed only from child method, not from the scope of functions that are called inside this method.Generator functions are still functions and support binding. This will result in bound function that cannot be identified as generator function by its signature, but as long as coroutine library supports generic iterators (
codoes), that's fine.So basically it is
Or even better:
There are several things that can be improved here. The first one is that it uses promise constructor antipattern.
coalready returns a promise, there's no need to wrap it withnew Promise.Another thing is that it is beneficial to separate generator method and promisified method, for the sake of seamless inheritance.
cosupports delegatedyields, it makes this even easier:Both TypeScript and Babel support ES2017
async..awaitand are are able to fall back toco-like generator coroutines in ES6 target output. This makescoefficiently useless in transpiled JS projects, the code above becomes: