async function with the class in javascript
16,548
Solution 1
You can simply add async before function name to declare that function as async,
class ApnService {
async sendNotification(deviceType, deviceToken, msg, type, id) {
try {
const note = await apnProvider.send(note, deviceToken)
console.log(note)
} catch (err) {
console.log(err)
}
}
}
export default ApnService
Solution 2
async
is a keyword to designate an asynchronous function, try
class ApnService {
async sendNotification(deviceType, deviceToken, msg, type, id) {
try {
const note = await apnProvider.send(note, deviceToken)
console.log(note)
} catch (err) {
console.log(err)
}
}
}
export default ApnService;
Solution 3
class Foo {
x = something
}
This assignment is an example of a class field. The usage of class property / class field syntax is currently at stage-3 in the TC39 process, meaning it is not yet in ECMAScript and not yet supported natively by all JS engines. It can be used via transpilers like Babel, but only if you configure and run such a transpiler yourself.
Luckily you don't need class field syntax to make a class method async, you can just use the async
keyword.
class Foo {
async myMethod () {/* ... */}
}
Author by
Dark Knight
Updated on July 18, 2022Comments
-
Dark Knight almost 2 years
I have create a class in nodejs
class ApnService { sendNotification(deviceType, deviceToken, msg, type, id) { try { const note = await apnProvider.send(note, deviceToken) console.log(note) } catch (err) { console.log(err) } } } export default ApnService
What I need to do is to convert above function to
async
. But when I use below syntax It throws me errorSyntaxError: src/services/apn.js: Unexpected token (43:19) 41 | } 42 | > 43 | sendNotification = async(deviceType, deviceToken, msg, type, id) => { | ^
Below is the syntax
class ApnService { sendNotification = async(deviceType, deviceToken, msg, type, id) => { try { const note = await apnProvider.send(note, deviceToken) console.log(note) } catch (err) { console.log(err) } } } export default ApnService
-
Dark Knight over 5 yearsYes this will work but this is not ES-7 syntax. Am I right?
-
Aniket Pawar over 5 years@DarkKnight Async functions are introduced in ES2017. If you are using es6 then you need to compile it using babel or webpack
-
Dark Knight over 5 yearsThis is my
.babelrc
{ "presets": [ ["env", { "targets": { "node": 4.3 } }] ], "env": { "test": { "plugins": ["istanbul"] } } }
Could you please let me know what I am missing here? -
Dark Knight over 5 yearsThis is my
.babelrc
{ "presets": [ ["env", { "targets": { "node": 4.3 } }] ], "env": { "test": { "plugins": ["istanbul"] } } }
Could you please let me know what I am missing here? -
Gabriel L. over 5 yearsOP isn't trying to use
async
as a function, they're using it as a keyword. Look again:sendNotification = async (/* args */) => {
-
Drew Reese over 5 yearsAh yes, narrow mobile screen.
-
Aniket Pawar over 5 yearsCan you tell more about version of ecmascript and Node that you are using currently ?
-
Dark Knight over 5 yearsnode version
v8.9.3
and How do I check thisversion of ecmascript
? -
Aniket Pawar over 5 yearsHave you tried above syntax ? you still have any errors ?
-
Dark Knight over 5 yearsAbove syntax works for me. But I know this is not the latest es7 syntax
-
Aniket Pawar over 5 yearsIf you want to know that which latest ecmascript features your current node version support than have a look at this website
-
Dark Knight over 5 years
async arrow functions
this should works as listed the link. Ok now I am asking a new question related to this. THanks for the help!!! -
Alexander Cherednichenko almost 5 yearswhat happens when you actually create at instance of this Class and try to call async method of this class? how would you 'await' for the result of this call?