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

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

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

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

Takashi Masuda

November 10, 2017
Tweet

More Decks by Takashi Masuda

Other Decks in Programming

Transcript

  1. ಉظॲཧͭΒ͍ (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͕ݻ·Δɻ
  2. ඇಉظॲཧ΋ͭΒ͍ (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ʢ൪ฌʣؔ਺͸ඞཁ
  3. ඇಉظॲཧ΋ͭΒ͍ (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Ͱؔ਺ఆٛ͸ লུͰ͖Δ
  4. ඇಉظॲཧ΋ͭΒ͍ (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࢖͏ͱ ͍ͣͿΜεοΩϦ͢Δɻ ͱ͜ΖͰԿ΍ͬͯͨΜ͚ͩͬʁ
  5. 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) ಡΈ΍͍͢ɾɾɾͷ͔ʁ
  6. 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) ෼͔Γ΍͍͢ɾɾɾʁ
  7. 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) ͖ͬ͢Γ͠ਿ಺
  8. 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) Τϥʔॲཧ͸͍ͭ΋Ͳ͓Γʂ