I get this error when I try to run an async function described in a class
masterClass.js
class MasterClass{
async function updateData(a, b){
let [ res1, res2 ] = await Promise.all(call1, call2);
return [ res1, res2 ]
}
}
test.js
const MasterClass = require('./MasterClass.js')
let m = new MasterClass()
m.updateData(a, b)
Error
async function updateData(a, b){
^^^^^^^^^^
SyntaxError: Unexpected identifier
Async Option ConstructorThe async function call can be added right into the class instantiation step, without needing a separate init() call or having to modify your established method of class construction.
async and awaitInside an async function, you can use the await keyword before a call to a function that returns a promise. This makes the code wait at that point until the promise is settled, at which point the fulfilled value of the promise is treated as a return value, or the rejected value is thrown.
The async keyword turns a method into an async method, which allows you to use the await keyword in its body. When the await keyword is applied, it suspends the calling method and yields control back to its caller until the awaited task is complete. await can only be used inside an async method.
You dont need function
as pointed out by @dfsq in the comments
Then you have to use module.exports
or export
to exposed your class as a module.
masterclass.js
module.exports = class MasterClass{
async updateData(a, b){
let [ res1, res2 ] = await Promise.all(call1, call2);
return [ res1, res2 ]
}
}
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