Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 那你就明白了_Javascript_Try Catch_Es6 Promise - Fatal编程技术网

Javascript 那你就明白了

Javascript 那你就明白了,javascript,try-catch,es6-promise,Javascript,Try Catch,Es6 Promise,我有一个使用knex promises的抽象概念: Core.js class CoreModel { constructor(tableName) { this.table = tableName; } add(payload, table) { return this.conn()(table ? table : this.table) .insert(payload) .then(id => id) .catch(err => getMsg

我有一个使用knex promises的抽象概念: Core.js

class CoreModel {
  constructor(tableName) {
  this.table = tableName;
}

add(payload, table) {
  return this.conn()(table ? table : this.table)
    .insert(payload)
    .then(id => id)
    .catch(err => getMsg(300, err))
    .finally(() => { this.conn().destroy() });
}
return this.Users.add(request.payload.users)
  .then(id => id)
  .catch(err => err);
我正在呼叫一名控制员:

register.js

class CoreModel {
  constructor(tableName) {
  this.table = tableName;
}

add(payload, table) {
  return this.conn()(table ? table : this.table)
    .insert(payload)
    .then(id => id)
    .catch(err => getMsg(300, err))
    .finally(() => { this.conn().destroy() });
}
return this.Users.add(request.payload.users)
  .then(id => id)
  .catch(err => err);
为什么在
core.js
中,它确实按预期到达捕获,但捕获没有传播到
catch
,而是传播到
中,然后在
register.js
中,我是否完全错过了一些错误

这真让我抓狂,很简单,但我搞不懂

提前感谢:D

您正在(添加)中捕获它,并且捕获的工作方式与正常函数中的工作方式相同——如果捕获它,它将消失

function add(a, b) {
    try {
        return a+b;
    } catch(e) {
      return something;
    }
}
在try-catch块中调用“add”时。你永远抓不到任何东西

如果希望错误继续存在,请执行以下操作

.catch(err => {getMsg(300, err); throw err;})

哦,天哪,我不知道!谢谢你,伙计,你在一分钟内帮了我两个小时的忙顺便说一句,另一种选择是
.catch(err=>Promise.reject(getMsg(300,err))
,这很有道理,但实际上我更喜欢在语法方面抛出“it's cleaner”。但我想这可能是基于我们想做什么。在这种情况下,我只是想要一个与抽象承诺的直接联系。谢谢,顺便说一句:D有其他选择总是很好嗨