async function with the class in javascript
Asked Answered
I

3

17

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 error

SyntaxError: 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
Ilarrold answered 15/11, 2018 at 6:18 Comment(0)
P
22

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
Positively answered 15/11, 2018 at 6:22 Comment(10)
Yes this will work but this is not ES-7 syntax. Am I right?Ilarrold
@DarkKnight Async functions are introduced in ES2017. If you are using es6 then you need to compile it using babel or webpackPositively
This is my .babelrc { "presets": [ ["env", { "targets": { "node": 4.3 } }] ], "env": { "test": { "plugins": ["istanbul"] } } } Could you please let me know what I am missing here?Ilarrold
Can you tell more about version of ecmascript and Node that you are using currently ?Positively
node version v8.9.3 and How do I check this version of ecmascript?Ilarrold
Have you tried above syntax ? you still have any errors ?Positively
Above syntax works for me. But I know this is not the latest es7 syntaxIlarrold
If you want to know that which latest ecmascript features your current node version support than have a look at this websitePositively
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!!!Ilarrold
what 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?Lucas
S
3

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;
Spermous answered 15/11, 2018 at 6:32 Comment(2)
OP isn't trying to use async as a function, they're using it as a keyword. Look again: sendNotification = async (/* args */) => {Urdu
Ah yes, narrow mobile screen.Spermous
U
1
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 () {/* ... */}
}
Urdu answered 15/11, 2018 at 6:27 Comment(1)
This is my .babelrc { "presets": [ ["env", { "targets": { "node": 4.3 } }] ], "env": { "test": { "plugins": ["istanbul"] } } } Could you please let me know what I am missing here?Ilarrold

© 2022 - 2024 — McMap. All rights reserved.