Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PromiseとAsync-Awaitまとめ(Emacsで)

 PromiseとAsync-Awaitまとめ(Emacsで)

フィードフォースの社内LT大会で発表しました

D6c5403c0b6ef2f9fd51910ea38323a3?s=128

Takashi Masuda

November 10, 2017
Tweet

More Decks by Takashi Masuda

Other Decks in Programming

Transcript

  1. 1SPNJTFͱ "TZOD"XBJU·ͱΊ !NBTVUBLB

  2. w օ͞Μ͝ଘ஌ͷ௨Γɺ&NBDTͰඇಉظ ॲཧΛ࣮ݱ͢ΔͨΊʹ͸ɺ5$1ίωΫ γϣϯΛ։͔͘ɺ֎෦ίϚϯυΛىಈ ͢Δඞཁ͕͋Γ·͢

  3. ֎෦ίϚϯυΛىಈ͢Δ

  4. w ΤσΟλͰԿ͔ॻ͍͍ͯΔ࣌ɺόοΫάϥ΢ϯ υͰԿ͔ͨ͘͠ͳΓ·͢ΑͶʁ w ྫ͑͹ɺ(JU)VCͰ4UBSͨ͠ϦϙδτϦ΍͸ͯϒ ͨ͠63-ΛΤσΟλ͔Β։ͨ͘ΊɺΩϟογϡ ϑΝΠϧΛDVSMͰఆظతʹ࡞Δͱ͔

  5. ಉظॲཧͭΒ͍ (defun mstk-start-sync () (call-process "sh" nil nil nil "-c"

    "sleep 3; echo OK") (message-box "mstk-start-sync finished.")) (mstk-start-sync) 3ඵؒԿ΋ग़དྷͣʹɺ Emacs͕ݻ·Δɻ
  6. ඇಉظॲཧ΋ͭΒ͍ (defun mstk-start-async-1a () (setq proc (start-process "mstk-start-async-1a" (generate-new-buffer "

    *sh*") "sh" "-c" "sleep 3; echo OK")) (set-process-sentinel proc 'mstk-async-sentinel-1a)) (defun mstk-async-sentinel-1a (_process _event) (message-box "mstk-start-async-1a finished.")) (mstk-start-async-1a) Emacs͸ݻ·Βͳ͘ͳΔ͕ɺ sentinelʢ൪ฌʣؔ਺͸ඞཁ
  7. ඇಉظॲཧ΋ͭΒ͍ (defun mstk-start-async-1b () (setq proc (start-process "mstk-start-async-1b" (generate-new-buffer "

    *sh*") "sh" "-c" "sleep 3; echo OK")) (set-process-sentinel proc (lambda (_process _event) (message-box "mstk-start- async-1b finished.")))) (mstk-start-async-1b) lambdaͰؔ਺ఆٛ͸ লུͰ͖Δ
  8. ඇಉظॲཧ΋ͭΒ͍ (defun mstk-start-async-2b () (make-process :name "mstk-start-async-2b" :buffer (generate-new-buffer "

    *sh*") :command '("sh" "-c" "sleep 3; echo OK") :sentinel (lambda (_process _event) (message-box "mstk-start- async-2b finished.")))) (mstk-start-async-2b) make-process࢖͏ͱ ͍ͣͿΜεοΩϦ͢Δɻ ͱ͜ΖͰԿ΍ͬͯͨΜ͚ͩͬʁ
  9. ֎෦ίϚϯυΛඇಉظͰ ࣮ߦ͍͚ͨͩ͠ͳͷʹʂ

  10. FNBDTQSPNJTF https://github.com/chuntaro/emacs-promise

  11. QSPNJTF (require 'promise) (defun mstk-start-promise-1a () (promise-chain (promise:make-process "sh" "-c"

    "sleep 3; echo OK") (then (lambda (reason) (message-box "then %s" reason))))) (mstk-start-promise-1a) ಡΈ΍͍͢ɾɾɾͷ͔ʁ
  12. QSPNJTF Τϥʔॲཧ (require 'promise) (defun mstk-start-promise-2a () (promise-chain (promise:make-process "sh"

    "-c" "sleep 3; echo OK") (then (lambda (reason) (/ 1 0) ;; an error occurs!! (message-box "then %s" reason))) (promise-catch (lambda (reason) (message-box "catch error!! %s" reason))) (done (lambda (reason) (message-box "done. %s" reason))))) (mstk-start-promise-2a) ෼͔Γ΍͍͢ɾɾɾʁ
  13. FNBDTBTZODBXBJU https://github.com/chuntaro/emacs-async-await

  14. BTZODBXBJU (require 'async-await) (setq lexical-binding t) (async-defun mstk-start-async-await-1a () (await

    (promise:make-process "sh" "-c" "sleep 3; echo OK")) (message-box "finish")) (mstk-start-async-await-1a) ͖ͬ͢Γ͠ਿ಺
  15. BTZODBXBJU Τϥʔॲཧ (require 'async-await) (setq lexical-binding t) (async-defun mstk-start-async-await-2a ()

    (condition-case reason (progn (await (promise:make-process "sh" "-c" "sleep 3; echo OK")) (/ 1 0) ;; an error occurs!! (message-box "finish")) (error (message-box "catch error!! %s" reason)))) (mstk-start-async-await-2a) Τϥʔॲཧ͸͍ͭ΋Ͳ͓Γʂ
  16. σϞ

  17. ॴײ w ҎલΑΓ΋ඇಉظॲཧͷ໘౗ष͞͸গͳ͘ͳͬ ͨͱײͨ͡ w γϯλοΫεΛఆٛͰ͖Δݴޠ͸ɺදݱͷࣗ༝ ౓͕ߴ·Δ w -JTQ͸σʔλ΋ؔ਺΋શ෦4ࣜͰදݱͰ͖Δɻඒ ͍͠

  18. એ఻ w IUUQTHJUIVCDPNNBTVUBLBFNBDTIFMN IBUFOBCPPLNBSL w IUUQTHJUIVCDPNNBTVUBLBFNBDTIFMNRJJUB w BTZODFM࢖ͬͨΒIFMNHJUIVCTUBSTFMΛมߋ ͤͣʹඇಉظԽͰ͖ͨIUUQTNBTVUBLBOFU DIBMPXIUNM

  19. 4QFDJBM5IBOLT w ஜࢵ"ؙΰγοΫʢຊจʣ w .FOMPʢίʔυʣ w IJHIMJHIUʢίʔυϋΠϥΠτʣ w IJHIMJHIU0SUGIPHFFMcQCDPQZ http://kakakakakku.hatenablog.com/entry/2017/05/30/092247