【问题标题】:ES6 syntax : what is this an error : .catch(err => throw err); [duplicate]ES6 语法:这是什么错误:.catch(err => throw err); [复制]
【发布时间】:2016-09-26 23:04:09
【问题描述】:

以下代码可以正常工作:

 pool.getConnection()
    .then((conn)=>{
         // something here
       })
      .catch((err) => {
      throw err;
    });

但是,当我重新格式化它时,节点会抛出错误:

return pool.getConnection()
    .then((conn) => {
     //something here
    })
    .then(results => results[0].insertId)
    .catch(err => throw err);  <-- NODE COMPALINS HERE


$>somefile.js line(190)
.catch(err => throw err);
                  ^^^^^
SyntaxError: Unexpected token throw

我在这里错过了什么?

【问题讨论】:

    标签: javascript


    【解决方案1】:

    箭头函数有一个行为,如果你不把{}放在它后面,它就会有一个隐含的return

    所以你的代码本质上是这样的:

    .catch(err => { return throw err; });
    

    只能返回表达式,不能返回throw之类的语句。

    function test(e) {
      return throw e; // syntax error
    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-26
      • 2017-02-23
      • 1970-01-01
      • 2018-06-17
      • 1970-01-01
      • 2016-03-24
      • 2019-12-28
      相关资源
      最近更新 更多