0

Environment: Node.js v7.2.0

I am trying to use async keyword to create method that returns a promise.

I get the following error:

SyntaxError: Unexpected token function

this.getFrame = async function(data) {
                      ^^^^^^^^

Here is the example:

function Parser() {
  this.receiveBuffer = Buffer.alloc(0);
  this.frameBuff = Buffer.alloc(0);

  /* GET FRAME ******************************** */
  this.getFrame = async function(data) { //The ERROR appears here!!!
    for (let byte of data) {
      if (byte !== 0x0d && byte !== 0x0a) {
        // console.log()
        this.receiveBuffer = Buffer.concat([
          this.receiveBuffer,
          Buffer.from([byte])
        ]);
        // console.log("Buffer is long: " + this.receiveBuffer.length);
      } else {
        this.frameBuff = this.receiveBuffer;
        this.receiveBuffer = Buffer.alloc(0);
        // console.log(this.frameBuff);
        return this.frameBuff;
      }
    }
  };
  /* ******************************** */

  this.parse = function(frame) {
    console.log(frame);
  };
}
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Hairi
  • 3,318
  • 2
  • 29
  • 68

0 Answers0