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 () {/* ... */}
}
Share:
16,548
Dark Knight
Author by

Dark Knight

Updated on July 18, 2022

Comments

  • Dark Knight
    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 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
    
  • Dark Knight
    Dark Knight over 5 years
    Yes this will work but this is not ES-7 syntax. Am I right?
  • Aniket Pawar
    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
    Dark Knight over 5 years
    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?
  • Dark Knight
    Dark Knight over 5 years
    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?
  • Gabriel L.
    Gabriel L. over 5 years
    OP isn't trying to use async as a function, they're using it as a keyword. Look again: sendNotification = async (/* args */) => {
  • Drew Reese
    Drew Reese over 5 years
    Ah yes, narrow mobile screen.
  • Aniket Pawar
    Aniket Pawar over 5 years
    Can you tell more about version of ecmascript and Node that you are using currently ?
  • Dark Knight
    Dark Knight over 5 years
    node version v8.9.3 and How do I check this version of ecmascript?
  • Aniket Pawar
    Aniket Pawar over 5 years
    Have you tried above syntax ? you still have any errors ?
  • Dark Knight
    Dark Knight over 5 years
    Above syntax works for me. But I know this is not the latest es7 syntax
  • Aniket Pawar
    Aniket Pawar over 5 years
    If you want to know that which latest ecmascript features your current node version support than have a look at this website
  • Dark Knight
    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
    Alexander Cherednichenko almost 5 years
    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?