問題:
I'm looking at this example from Angular's docs for $q
but I think this probably applies to promises in general.我正在查看 Angular 文檔中$q
這個例子,但我認爲這可能適用於一般的承諾。 The example below is copied verbatim from their docs with their comment included:下面的例子是從他們的文檔中逐字複製的,包括他們的評論:
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
I'm not clear how this works.我不清楚這是如何工作的。 If I can call .then()
on the result of the first .then()
, chaining them, which I know I can, then promiseB
is a promise object, of type Object
.如果我可以對第一個.then()
的結果調用.then()
,將它們鏈接起來,我知道我可以,那麼promiseB
是一個承諾對象,類型爲Object
。 It is not a Number
.它不是一個Number
。 So what do they mean by "its value will be the result of promiseA incremented by 1"?那麼他們所說的“它的值將是 promiseA 加 1 的結果”是什麼意思?
Am I supposed to access that as promiseB.value
or something like that?我應該以promiseB.value
或類似的方式訪問它嗎? How can the success callback return a promise AND return "result + 1"?成功回調如何返回承諾並返回“結果 + 1”? I'm missing something.我錯過了一些東西。
解決方案:
參考一: https://en.stackoom.com/question/1zqYo參考二: https://stackoom.com/question/1zqYo