Created
November 12, 2017 13:36
-
-
Save masutaka/9365590357e5899876da5f869bc22325 to your computer and use it in GitHub Desktop.
Introduce promise and async-await on emacs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun mstk-start-sync () | |
(call-process "sh" nil nil nil "-c" "sleep 3; echo OK") | |
(message-box "mstk-start-sync finished.")) | |
(mstk-start-sync) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun mstk-start-async-2a () | |
(make-process :name "mstk-start-async-2a" | |
:buffer (generate-new-buffer " *sh*") | |
:command '("sh" "-c" "sleep 3; echo OK") | |
:sentinel 'mstk-async-sentinel-2a)) | |
(defun mstk-async-sentinel-2a (process _event) | |
(message-box "mstk-start-async-2a finished.")) | |
(mstk-start-async-2a) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment