いまさら振り返るPromise

 いまさら振り返るPromise

00580f6c11851d2fff0b3e2f7392226b?s=128

N. Shimizu

July 23, 2017
Tweet

Transcript

  1. いまさら振り返るPromise N.Shimizu (chikoski@gmail.com)

  2. Promise: いずれされる処理を表すオブジェクト https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Promise 1SPNJTF UIFO GVM'JMMFE UIFO SFKFDUFE  DBUDI

    SFKFDUFE 1SPNJTF Pending Fulfill Reject Return Return
  3. Promise chain GFUDI IUUQTFYBNQMFDPNFOEQPJOUTPNFBDUJPO  UIFO SFTQPOTFSFTQPOTFKTPO FSSOPUJGZ&SSPS FSS 

    UIFO EBUBEBUBMFOHUI EBUB1SPNJTFSFKFDU OPEBUB  UIFO EBUBSFOEFS%BUB EBUB FSSOPUJGZ/P%BUB 
  4. 処理を始めるところと、結果を利用するところを分けられる EP4PNFUIJOH QBSBN SFTQPOTF\ JG SFTQPOTFFSS \ SFUVSOIBOEMF&SSPS SFTQPOTFFSS 

    ^ EP"  EP#  EP$  EP%  DPOTVNF SFTQPOTFSFTVMU  ^  DPOTUQEP4PNFUJOH QBSBN  EP"  EP#  EP$  EP%  QUIFO DPOTVNF DBUDI IBOEMF&SSPS
  5. Promise: 条件分岐の一種 https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Promise 1SPNJTF UIFO GVM'JMMFE UIFO SFKFDUFE  DBUDI

    SFKFDUFE 1SPNJTF Pending Fulfill Reject Return Return
  6. 直列化 UFTU" UIFO UFTU#  UIFO TVDDFTT DBUDI GBJM 

    <UFTU" UFTU#> SFEVDF QSP GVOD QSPUIFO GVOD  UIFO TVDDFTT DBUDI GBJM  1SPNJTFBMM <UFTU" UFTU# > UIFO TVDDFTT DBUDI GBJM  㛈ᏁҶ੢⼊ҿӶӔҮ UFTU" UFTU# TVDDFTT GBJM F T T F
  7. 並列化 UFTU" DBUDI UFTU#  UIFO TVDDFTT DBUDI GBJM 

    1SPNJTFSBDF <UFTU" UFTU# > UIFO TVDDFTT DBUDI GBJM  㛈ᏁҶ੢⼊ҿӶӔҮ UFTU" UFTU# TVDDFTT GBJM F T T F
  8. エラー処理を本筋と分けて実装できる MFUSFTVMUEP4PNFUIJOH  JG SFTVMUOVMM \ SFTVMUEFGBVMU7BMVF  ^ DPOTVNF

    SFTVMU   EP4PNFUIJOH  DBUDI FSSEFGBVMU7BMVF  UIFO DPOTVNF 
  9. Promise非対応のPromise対応にするには GVODUJPOBEE B C \ SFUVSOB C ^ GVODUJPOQSPNJTFE"EE B

    C \ SFUVSO1SPNJTFSFTPMWF B C  ^ QSPNJTFE"EE   UIFO JDPOTPMFMPH   
  10. Promise非対応のPromise対応にするには GVODUJPOBEE B C \ JG B]]C \ UISPXOFX&SSPS *OWBMJEQBSBNFUFS

     ^ SFUVSOB C ^ GVODUJPOQSPNJTFE"EE B C \ USZ\SFUVSO1SPNJTFSFTPMWF BEE B C ^ DBUDIFSS\SFUVSO1SPNJTFSFKFDU FSS ^ ^
  11. Promise非対応のPromise対応にするには DPOTUGTSFRVJSF GT  GVODUJPOSFBE GJMF \ SFUVSOOFX1SPNJTF SFTPMWF SFKFDU

    \ GTSFBE'JMF GJMF  FSS EBUB \ FSS SFTPMWF FSS SFKFDU EBUB  ^ ^ ^ 
  12. Promise非対応のPromise対応にするには GVODUJPOBEE0OF OVNCFST \ DPOTUSFTVMU<> GPS DPOTUOVNPGOVNCFST \ SFTVMUQVTI OVN

      ^ ^ GVODUJPOQSPNJTFE"EE0OF OVNCFST \ SFUVSO1SPNJTFBMM OVNCFSTNBQ J1SPNJTFSFTPMWF J   ^
  13. Anti-pattern: ネストしたPromise EP" UIFO SFTVMU"\ EP# UIFO SFTVMU#DPOTVNF SFTVMU" SFTTVMU#

     ^  1SPNJTFBMM <EP" EP# >  UIFO SFTVMUTDPOTVNF SFTVMUT<> SFTVMU<>  http://taoofcode.net/promise-anti-patterns/
  14. Anti-pattern: 続かないチェーン GVODUJPOEP4PNFUIJOH \ DPOTUQSPNJTFEP"  QSPNJTFUIFO SFTVMUDPOTVNF SFTVMU 

    SFUVSOQSPNJTF ^ GVODUJPOEP4PNFUIJOH \ SFUVSOEP" UIFO SFTVMUDPOTVNF SFTVMU  ^ http://taoofcode.net/promise-anti-patterns/