1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【技術書まとめ】『JavaScript Promiseの本』を読んだまとめ

Posted at

Chapter.1 - Promiseとは何か

  • E言語で発見された
    • 並列/並行処理の言語デザインの一種
  • コールバックでエラーを返して、if (error) {}するのとの違い
    • あくまでコーディングルール
      • Promise はthencatchしか使えない
        • 統一される
          • 非同期処理をうまくパターン化する

2. Chapter.2 - Promiseの書き方

  • Promiseのショートカット
    • テストコードに使える
      • Promise.resolve
      • Promise.reject
  • thenableなオブジェクト
    • .thenを持っている
      • Array like みたいなもの
    • jQuery.ajax()の返り値
      • Promise.resolveでpromiseオブジェクトにできる
  • Promise#finally
    • 引数を受け取らない
      • どんな引数を返しても影響なし
    • isLoadingで使う
  • thenは常に新しいpromiseオブジェクトを返す
  • Promise.all
    • promiseをまとめる
      • 全て並列に実行される
  • Promise.race
    • 最初にFulfilledになった時点でthenが呼ばれる
      • 他のpromiseはキャンセルされない

4. Chapter.4 - Advanced

  • throw でなく reject する
    • throw が意図したものか例外なのかわからない

Chapter.5 - Async Function

  • Async Function
    • 必ずPromiseインスタンスを返す
      • Promise.resolve(返り値)を返す
    • awaitが使える
      • 外の処理は止まらない
  • await
    • 右辺のPromiseインスタンスがFullfilledRejectedになるまで待つ
  • Async Functionと配列
    • forを使う
      • 順番が重要でなければPromise.allを使う
    • forEachは使えない
      • awaitAsync Functionの直下でしか使えないから
        • コールバックにするとどうか?
          • 外側の処理が先に進んでしまう
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?