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

3 Answers 3

23

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
Sign up to request clarification or add additional context in comments.

10 Comments

Yes this will work but this is not ES-7 syntax. Am I right?
@DarkKnight Async functions are introduced in ES2017. If you are using es6 then you need to compile it using babel or webpack
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?
Can you tell more about version of ecmascript and Node that you are using currently ?
node version v8.9.3 and How do I check this version of ecmascript?
|
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;

2 Comments

OP isn't trying to use async as a function, they're using it as a keyword. Look again: sendNotification = async (/* args */) => {
Ah yes, narrow mobile screen.
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 () {/* ... */}
}

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.