async function with the class in javascript

await javascript
async/await javascript
async arrow function
nodejs async/await
async/await fetch
async/await try/catch
async/await vs promises
javascript promise

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

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

Making asynchronous programming easier with async and await , How to specify async class method in node.js 7.6 + The following ways are breaking : class A{ async abc(){ // return 'abc'; } } and class A{ async  Async functions are normal JavaScript functions with the following differences — An async function always returns a promise. async function fn() {return 'hello';} fn().then(console.log) // hello. The function fn returns 'hello'. Because we have used async, the return value 'hello' is wrapped in a promise (via Promise constructor).

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;

async await for class method · Issue #11688 · nodejs/node · GitHub, Async function is coroutine that allow to work with Promises by using javascript language syntax and allow to implement nonlinear control flow in javascript. Currently async control flow in javascript can be expressed with several features: event, callback, streams and promises, and soon with async generator. Async. Async functions enable us to write promise based code as if it were synchronous, but without blocking the execution thread. It operates asynchronously via the event-loop. Async functions

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 () {/* ... */}
}

2. Async Functions in JavaScript: async / await, Yes, you can use asynchronous methods (even with the proposed async syntax) on classes, and getters can return promises as well. However, you will need to decide what should happen when a method is called while some asynchronous process is still active. In summary, async/await is a cleaner syntax to write asynchronous Javascript code. It enhances readability and flow of your code. Things to keep in mind while using async/await : Async functions return a promise.

Are JavaScript ES6 Classes of any use with asynchronous code , Class Methods as async. A class method can also be async, just put async before it. // server.js class  This JavaScript tutorial help to understand async-await concept.The asynchronous function help to run tasks or process parallel without any callback hell. The JavaScript is single thread programming language.That means only one operation can be carried out at a time.

Javascript async-await Example, automatically wraps it in a resolved promise with its value. The word “async” before a function means one simple thing: a function always returns a promise. Other values are wrapped in a resolved promise automatically. For instance, this function returns a resolved promise with the result of 1; let’s test it: async function f() { return 1; } f().then(alert); // 1.

How to use Async Await in JavaScript., automatically wraps it in a promise which is resolved with its value. An async function is a function which knows how to expect the possibility of the await keyword being used to invoke asynchronous code. Try typing the following lines into your browser's JS console: function hello() { return "Hello" }; hello();

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?
  • 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.
  • 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?