Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.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 在then()块内使用返回承诺的函数_Javascript_Node.js_Promise - Fatal编程技术网

Javascript 在then()块内使用返回承诺的函数

Javascript 在then()块内使用返回承诺的函数,javascript,node.js,promise,Javascript,Node.js,Promise,我有一个承诺链如下: return performTaskA(). then(performTaskB). then(performTaskC). then(performTaskD). then(performTaskE); function performTaskD() { Model.something(). then(function(result) { something something with result; //BREAKPOINT 1

我有一个承诺链如下:

return performTaskA().
then(performTaskB).
then(performTaskC).
then(performTaskD).
then(performTaskE);
function performTaskD() {
    Model.something().
    then(function(result) { 
         something something with result; //BREAKPOINT 1
    }); 
}
AccountModel.findById(acctId).
    then(function (account) {
        destAccount = account; //destAccount is a var declared in the outer scope.
});
performTaskD
如下所示:

return performTaskA().
then(performTaskB).
then(performTaskC).
then(performTaskD).
then(performTaskE);
function performTaskD() {
    Model.something().
    then(function(result) { 
         something something with result; //BREAKPOINT 1
    }); 
}
AccountModel.findById(acctId).
    then(function (account) {
        destAccount = account; //destAccount is a var declared in the outer scope.
});
当我运行上面的承诺链时,断点1永远不会被命中,控件继续执行
performTaskE
。 但是,当我单独调用函数
performTaskD()
时,断点1确实会被命中。在承诺链的情况下,我做错了什么

如果我在
performTaskD
中返回承诺,我仍然有相同的问题。唯一的区别是控件从不进入
performTaskE
,进程退出

为清楚起见,
performTaskD
如下所示:

return performTaskA().
then(performTaskB).
then(performTaskC).
then(performTaskD).
then(performTaskE);
function performTaskD() {
    Model.something().
    then(function(result) { 
         something something with result; //BREAKPOINT 1
    }); 
}
AccountModel.findById(acctId).
    then(function (account) {
        destAccount = account; //destAccount is a var declared in the outer scope.
});

返回
承诺s

function performTaskD() {
    return Model.something().
    then(function(result) { 
         return something something with result; //BREAKPOINT 1
    }); 
}
根据
模型。查找(某物)
不会返回承诺。您需要调用
Model.find(something).exec()
performTaskD
应该类似于:

function performTaskD(AcctId) {
  return AccountModel.findById(AcctId).exec().
  then(function (account) {
    destAccount = account;
    return account;
  });
}
解析承诺时调用“then”函数。在任务函数中,您在函数本身内部处理承诺,因此链的其余“then”不会被调用


您应该
返回
来自
performTaskD
的承诺,这样它就会被正确地链接起来,向我们展示
模型的功能。显然,它在执行其他任务时存在问题。另外,您是否尝试添加错误处理程序?@Bergi Model.something只是一个mongoose方法,它使用承诺返回
find()
。@Bergi还添加了一个.catch()。它永远不会被击中。那样的话,它听起来像只猫鼬。但是您是否也在
Model.something()
上设置了断点,并确保它确实被调用了?可能是链中以前的一个承诺没有实现。如果我在performTaskD中返回该承诺,我仍然有相同的问题。唯一的区别是控件从不继续执行PerformTask,进程退出。建议在链的末尾也包括
.catch()
,以确定问题所在。我这样做了。.catch()永远不会命中什么是“具有结果的某物”?在外部块中为结果指定一个变量<代码>x=结果其中x是在封闭函数中声明的变量。当您没有传递回调时,它会返回一个thenable,这就足够了。@Bergi是的,但您不能链接thenable。您需要一个实际的承诺…一个其
的表。然后
方法返回一个承诺。它应该是现成的,唯一缺少的是
Promise.prototype
中的其他方法。