Skip to content

Commit d596921

Browse files
committed
fix(promise-finally): コード例を修正
1 parent 2430eae commit d596921

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

Ch2_HowToWrite/promise-finally.adoc

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,11 @@ Promise.reject(new Error("失敗")).finally(function(){
2121
----
2222

2323
`finally` メソッドのコールバック関数は引数を受け取らず、どのような値を返してもpromise chainには影響を与えません。
24-
また、 `finally` メソッドも新しいpromiseオブジェクトを返し、新しいpromiseオブジェクトは呼び出し元のpromiseオブジェクトの状態をそのまま引き継ぎます。
25-
26-
`Promise#finally(onFinally)` は、`Promise#then` メソッドで書くと次のように書けます。
24+
また、 `finally` メソッドは新しいpromiseオブジェクトを返し、新しいpromiseオブジェクトは呼び出し元のpromiseオブジェクトの状態をそのまま引き継ぎます。
2725

2826
[role="executable"]
2927
[source,javascript]
30-
.finallyをthenで表現
28+
.finallyとpromise chain
3129
----
3230
function onFinally() {
3331
// 成功、失敗どちらでも実行したい処理
@@ -41,8 +39,18 @@ Promise.resolve(42).finally(function(){
4139
// 呼び出し元のpromiseオブジェクトは `42` で resolveされている
4240
console.log(value); // 42
4341
})
42+
----
43+
44+
`Promise#finally` メソッドと同等の表現を `Promise#then` メソッドで書くと次のように書けます。
45+
46+
[source,javascript]
47+
.finallyをthenで表現
48+
----
49+
function onFinally() {
50+
// 成功、失敗どちらでも実行したい処理
51+
}
4452
45-
// Promise#thenでの同等の表現
53+
// Promise#finally(onFinally) と同等の表現
4654
promise.then(function(result) {
4755
onFinally();
4856
return result;
@@ -52,7 +60,6 @@ promise.then(function(result) {
5260
});
5361
----
5462

55-
5663
`Promise#finally` メソッドを使うことで、promise chainで必ず実行したい処理を簡単に書けるようになっています。
5764

5865
次のコードでは、リソースを取得中かどうかを判定するためのフラグを `isLoading` という変数で管理しています。

0 commit comments

Comments
 (0)