skeeto / emacs-aio

async/await for Emacs Lisp
The Unlicense
217 stars 12 forks source link

Understanding aio-defun #25

Open Silex opened 2 years ago

Silex commented 2 years ago

Hello,

The following code works:

(defun aio-call-process (program &rest args)
  (let* ((process (apply #'start-file-process program "*aio-call-process*" program args))
         (promise (aio-promise)))
    (prog1
        promise
      (setf (process-sentinel process) (-partial #'aio-process-sentinel promise)))))

(defun aio-process-sentinel (promise proc status)
  "Sentinel that resolves the PROMISE using PROC and STATUS."
  (aio-resolve
   promise
   (lambda ()
     (with-current-buffer (process-buffer proc)
       (prog1
           (buffer-substring-no-properties (point-min) (point-max))
         (kill-buffer))))))

(aio-defun aio-run (command &rest args)
  (interactive)
  (message "Start")
  (message (aio-await (apply #'aio-call-process command args)))
  (message "Stop"))

(aio-run "echo" "123")

But if I just change (defun aio-call-process (program &rest args) to (aio-defun aio-call-process (program &rest args) then nothing works anymore. I'm confused, can you clarify why?

I thought aio-defun was to enable aio-await but that it was still somewhat of a normal function.

Also, apparently I'm supposed to use aio-make-callback to simplify the code above but honestly I have no clue how :sweat_smile:

justinbarclay commented 1 year ago

I am definitely running into the last line here - I'm curious how aio-make-callback simplifies working with start-process or make-process