I'm playing a bit with async/await of Node 8.3.0 and I have some issue with static function.
MyClass.js
class MyClass {
static async getSmthg() {
return true;
}
}
module.exports = MyClass
index.js
try {
const result = await MyClass.getSmthg();
} catch(e) {}
With this code I've got an SyntaxError: Unexpected token
on MyClass
.
Why is that? Can't use a static function with await
or have I made a mistake?
Thank you
The await operator can only be used inside a async function if your node or browser don't support top level await and it doesn't run as a module.
You would have to do this instead
(async () => {
try {
const result = await MyClass.getSmthg();
} catch(e) {}
})()
the alternative can be to set "type": "module"
in package.json
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