4

I'm writing JavaScript (> ECMAScript 6) and I can't figure how to call a super class' async method in an extending class' method. This is what I'm trying to do:

class SuperClass {
    constructor(){}

    async method() {
        return;
    }
}

class ExtendClass extends SuperClass {
    constructor() {
        super();
    }

    async method() {
        return super.method();
    }
}

The above won't compile, getting this as error:

SyntaxError: 'super' keyword unexpected here
    at Object.<anonymous> (Path/To/File.js:line:character)

Am I actually trying to do something that isn't possible? Can't seem to Google anything useful..

It does not help to await the call to the super class, it does not help to have different method names - the only thing that helps is making the extending class' method non-async.

zniwalla
  • 367
  • 4
  • 17
  • it works fine in mine – zabusa Jan 22 '18 at 10:21
  • which version of node are you using? – zabusa Jan 22 '18 at 10:22
  • v8.9.4 - I am however using a testing framework called TestCafe; it's using ECMAScript >= 6, so it should behave as wanted.. hmm. – zniwalla Jan 22 '18 at 10:26
  • its a syntax error right.so the version not supporting this feature.i think you should compile to es5 and run – zabusa Jan 22 '18 at 10:30
  • But it is possible to compile it without the two async keywords. `super` is not the problem, even though that's what it says. – zniwalla Jan 22 '18 at 10:33
  • It should work as expected. Even if the problem is real, it's specific to your environment, while the question implies that it isn't so. – Estus Flask Jan 22 '18 at 10:36
  • 3
    Does that testing framework do any kind of transpilation? – Bergi Jan 22 '18 at 10:37
  • Ya, it must do some kind of transpilation, I believe. I will take this to their forum instead. – zniwalla Jan 22 '18 at 10:41
  • The error is likely possible if it was compiled to ES6, because in this case async methods will be transformed and `super` will appear in wrong place. I guess changing compilation target to ES5 would solve that. – Estus Flask Jan 22 '18 at 10:42
  • I think you are right. I can't seem to change compilation target, though. – zniwalla Jan 22 '18 at 10:50
  • I'd expect it's possible to edit Testcafe config file to force `es2015` Babel preset somehow, this would result in ES5 output. Yes, this is a question for Testcafe repo issues. – Estus Flask Jan 22 '18 at 11:13
  • Yes, this sounds likely. I will look for such a file, but the documentation of this library is not so rich as of yet - I will try their forum. Thanks for all the comments. – zniwalla Jan 22 '18 at 11:46

1 Answers1

3

This is due to the transpilation used in the testing framework, TestCafe (as stated by @Bergi). I will direct the question elsewhere. Thanks for the comments.

UPDATE

This is currently a bug in the TestCafe framework (link to bug). The workaround is as follows:

async method () {
    return await SuperClass.prototype.method.call(this)
}
zniwalla
  • 367
  • 4
  • 17