-
-
Notifications
You must be signed in to change notification settings - Fork 223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(async): エラーファーストコールバックをコラムへ移動 #1457
Conversation
✅ Deploy Preview for js-primer ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
source/basic/async/README.md
Outdated
エラーファーストコールバックは、ES2015でPromiseが標準化されるまでは広く使われており、特にNode.jsでは幅広く使われていました。 | ||
しかし、エラーファーストコールバックは非同期処理におけるエラーハンドリングの書き方を決めた**ただのルール**であって仕様ではありません。 | ||
そのため、エラーファーストコールバックというルールを破っても、問題があるわけではありません。 | ||
また、エラーファーストコールバックはPromiseとは異なり、コールバック内で例外が発生した場合に自動的にエラーをキャッチできる仕組みはありません。 | ||
|
||
Promiseが標準化されて以降は、多くの非同期APIはPromiseベースのAPIとして提供されています。 | ||
これはNode.jsのコアモジュールも同様で、現在(Node.js v{{book.nodeversion}})ではPromiseベースのAPIが提供されています。 | ||
そのため、ほとんどの場面ではエラーファーストコールバックの代わりにPromiseとAsync Functionを利用して非同期処理を扱えるようになっています。 | ||
|
||
エラーファーストコールバックは、ルールがあるだけのただの関数であり処理的にも単純でパフォーマンスが最大限でることから、部分的に利用されている場面もあります。 | ||
しかし、PromiseとAsync FunctionはECMAScriptで仕様化されたものであり構文的なサポートもあるため、現在では非同期APIの主流となっています。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
この辺も半分ぐらいの分量にしたい
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
非同期処理の確率的な失敗についてを追加
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
up
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup
source/basic/async/README.md
Outdated
非同期処理の結果を扱う場合は、`asyncPromiseTask`関数が返す`Promise`インスタンスに対して、`then`や`catch`メソッドを使い、成功時や失敗時に呼び出される処理を登録します。 | ||
`asyncPromiseTask`関数で行った非同期処理が成功した場合は、返された`Promise`インスタンスの`then`メソッドで登録したコールバックが呼び出されます。 | ||
一方で、`asyncPromiseTask`関数で行った非同期処理が失敗した場合は、返された`Promise`インスタンスの`catch`メソッドで登録したコールバックが呼び出されます。 | ||
|
||
このように、Promiseをつかった非同期処理では、非同期処理をする側は`Promise`インスタンスを返し、結果を受け取る側は`then`や`catch`メソッドでその結果を受け取ります。 | ||
同期処理の関数では、関数を呼び出すと処理結果がそのまま返されますが、Promiseでの非同期処理では処理結果の代わりにまずは`Promise`インスタンスが返されます。 | ||
そして、非同期処理が完了したタイミングで、`Promise`インスタンスの`then`や`catch`メソッドで登録しておいたコールバック関数に処理結果が渡されます。 | ||
|
||
Promiseに慣れるまで少しややこしいように見えますが、Promiseは非同期処理の状態や結果をラップしたようなオブジェクトです。 | ||
同期的な関数では関数を実行した時点で結果がわかりますが、非同期な関数では関数を実行した時点ではまだ結果はわかりません。 | ||
そのため、非同期な関数は一旦Promiseと言う非同期処理の状態や結果をラップしたオブジェクトを返し、その結果が決まったら登録しておいたコールバック関数に結果を渡すという仕組みです。 | ||
|
||
実際に`Promise`インスタンスの使い方やどのように非同期処理を扱うかを見ていきます。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- コードの解説
- 少し抽象化したコードの解説
- 文字的な解説
流石に冗長な感じはするので、2/3ぐらいにまとめられるといいかな。
process.exit(1); | ||
return; | ||
} | ||
fs.readFile(filePath, { encoding: "utf8" }).then(file => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mjsじゃないからTop-Level awaitは扱えない。
@lacolaco CLIの方も一緒に書き換えました。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ありがとうございます コメントしたところ以外は違和感なかったです。ただエラーファーストコールバックの説明は文章にするとやっぱり冗長さが少しありますね
source/basic/async/README.md
Outdated
## [コラム] エラーファーストコールバック {#error-first-callback} | ||
|
||
ECMAScript 2015(ES2015)でPromiseが仕様に入るまで、非同期処理中に発生した例外を扱う仕様はありませんでした。 | ||
このため、ES2015より前までは、**エラーファーストコールバック**という非同期処理中に発生した例外を扱う方法を決めたルールが広く使われていました。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
この段落の背景の話、後にも似たようなことを言うから冒頭にはいらないかもしれない
Co-authored-by: Suguru Inatomi <suguru.inatomi@gmail.com>
Co-authored-by: Suguru Inatomi <suguru.inatomi@gmail.com>
エラーファーストコールバックをコラムに移動する。
非同期の章とNode CLIの章 どちらもPromiseが最初に来るようにして、エラーファーストコールバックはコラム欄に移動する。
プレビュー
fix #1453