异步在方法(函数)中给出语法错误

时间:2018-09-29 15:01:26

标签: javascript node.js

环境: Node.js v7.2.0

我正在尝试使用async关键字创建返回承诺的方法。

我收到以下错误:

  

SyntaxError:意外的令牌功能

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

这里是示例:

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);
  };
}

0 个答案:

没有答案