【发布时间】:2014-10-12 10:34:43
【问题描述】:
https://docs.angularjs.org/api/ng/service/$q
从 $q API 的文档中,then(successCallback, errorCallback, notifyCallback) 返回一个新的 Promise,该 Promise 通过 successCallback, errorCallback 的返回值解决或拒绝。
但是,该网站给出了一个没有明确调用resolve的示例
promiseB = promiseA.then(function(result) {
return result + 1;
});
// promiseB will be resolved immediately after promiseA is resolved and its value
// will be the result of promiseA incremented by 1
所以调用 then 函数会创建一个新派生的 Promise,但是由于代码没有调用 resolve() 或 reject(),$q 如何知道它的状态?但是既然网站说已经解决了,所以我假设
return result + 1;
隐式调用 resolve() 是否正确?因此,如果我不显式调用 reject() 并继续调用 then() 并返回一些值,则 Promise 将始终处于解析状态,但我不需要显式调用 resolve?
【问题讨论】:
-
$q在promiseA被解析时收到有关新状态的通知,例如当调用.resolve()的延迟时 -
“技术”术语是
.then既是map也是flatMap我猜。