Skip to content
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

Merged
merged 21 commits into from
Aug 20, 2022

Conversation

azu
Copy link
Collaborator

@azu azu commented Aug 6, 2022

エラーファーストコールバックをコラムに移動する。

非同期の章とNode CLIの章 どちらもPromiseが最初に来るようにして、エラーファーストコールバックはコラム欄に移動する。

  • エラーファーストコールバックをコラムに移動
  • Promiseをエラーファーストコールバックに依存しないように書き換え
  • node cliをPromiseベースに変更

プレビュー

fix #1453

@bot-user
Copy link

bot-user commented Aug 6, 2022

Deploy Preview for js-primer ready!

Name Link
🔨 Latest commit 0124643
🔍 Latest deploy log https://app.netlify.com/sites/js-primer/deploys/6300835a3bd5bb0008ddf109
😎 Deploy Preview https://deploy-preview-1457--js-primer.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@azu azu changed the title fix(async): エラーファーストコールバックを移動 fix(async): エラーファーストコールバックをコラムへ移動 Aug 6, 2022
Comment on lines 1792 to 1802
エラーファーストコールバックは、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の主流となっています。
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この辺も半分ぐらいの分量にしたい

Copy link
Collaborator Author

@azu azu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

非同期処理の確率的な失敗についてを追加

Copy link
Collaborator Author

@azu azu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

up

Copy link
Collaborator Author

@azu azu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup

Comment on lines 272 to 284
非同期処理の結果を扱う場合は、`asyncPromiseTask`関数が返す`Promise`インスタンスに対して、`then`や`catch`メソッドを使い、成功時や失敗時に呼び出される処理を登録します。
`asyncPromiseTask`関数で行った非同期処理が成功した場合は、返された`Promise`インスタンスの`then`メソッドで登録したコールバックが呼び出されます。
一方で、`asyncPromiseTask`関数で行った非同期処理が失敗した場合は、返された`Promise`インスタンスの`catch`メソッドで登録したコールバックが呼び出されます。

このように、Promiseをつかった非同期処理では、非同期処理をする側は`Promise`インスタンスを返し、結果を受け取る側は`then`や`catch`メソッドでその結果を受け取ります。
同期処理の関数では、関数を呼び出すと処理結果がそのまま返されますが、Promiseでの非同期処理では処理結果の代わりにまずは`Promise`インスタンスが返されます。
そして、非同期処理が完了したタイミングで、`Promise`インスタンスの`then`や`catch`メソッドで登録しておいたコールバック関数に処理結果が渡されます。

Promiseに慣れるまで少しややこしいように見えますが、Promiseは非同期処理の状態や結果をラップしたようなオブジェクトです。
同期的な関数では関数を実行した時点で結果がわかりますが、非同期な関数では関数を実行した時点ではまだ結果はわかりません。
そのため、非同期な関数は一旦Promiseと言う非同期処理の状態や結果をラップしたオブジェクトを返し、その結果が決まったら登録しておいたコールバック関数に結果を渡すという仕組みです。

実際に`Promise`インスタンスの使い方やどのように非同期処理を扱うかを見ていきます。
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • コードの解説
  • 少し抽象化したコードの解説
  • 文字的な解説

流石に冗長な感じはするので、2/3ぐらいにまとめられるといいかな。

@azu azu requested a review from lacolaco August 14, 2022 14:16
process.exit(1);
return;
}
fs.readFile(filePath, { encoding: "utf8" }).then(file => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mjsじゃないからTop-Level awaitは扱えない。

@azu
Copy link
Collaborator Author

azu commented Aug 14, 2022

@lacolaco CLIの方も一緒に書き換えました。
コラムってこんなイメージであってますかね?

Copy link
Collaborator

@lacolaco lacolaco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます コメントしたところ以外は違和感なかったです。ただエラーファーストコールバックの説明は文章にするとやっぱり冗長さが少しありますね

source/use-case/nodecli/read-file/README.md Outdated Show resolved Hide resolved
## [コラム] エラーファーストコールバック {#error-first-callback}

ECMAScript 2015(ES2015)でPromiseが仕様に入るまで、非同期処理中に発生した例外を扱う仕様はありませんでした。
このため、ES2015より前までは、**エラーファーストコールバック**という非同期処理中に発生した例外を扱う方法を決めたルールが広く使われていました。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この段落の背景の話、後にも似たようなことを言うから冒頭にはいらないかもしれない

azu and others added 2 commits August 20, 2022 14:06
Co-authored-by: Suguru Inatomi <suguru.inatomi@gmail.com>
Co-authored-by: Suguru Inatomi <suguru.inatomi@gmail.com>
@azu azu merged commit 0fe08a6 into master Aug 20, 2022
@azu azu deleted the feature/1453 branch August 20, 2022 06:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

非同期: PromiseとAsync Functionを中心とした内容に置き換える
3 participants