kiwanami / emacs-deferred

Simple asynchronous functions for emacs lisp
GNU General Public License v3.0
312 stars 43 forks source link

define 'cancel' #22

Closed thomp closed 10 years ago

thomp commented 10 years ago

Currently, the documentation doesn't clearly indicate what "cancelling" involves. It probably wouldn't hurt to be explicit what does and what doesn't occur with cancelling. For example, a modified version of an example from the documentation reveals that cancelling does not imply that things halt or are terminated in any way (the WAIT continues and the subsequent NEXTC does its thing as well):

(deferred:$
  (deferred:earlier
    (deferred:process "sh" "-c" "sleep 3 | echo 'hello!'")
    (deferred:$
      (deferred:wait 4000) ; timeout msec
      (deferred:nextc it (message "canceled!"))))
  (deferred:nextc it
    (lambda (x) (insert x))))
kiwanami commented 10 years ago

Thank you for your comment, and I'm sorry for my late response. Your pointing out is correct. The documentation should be fixed at two points.

First, the function deferred:earlier does not perform cancellation, just ignore the results which are arrived after the first result. (At first, I had thought that I would implement cancellation, but I have found that it is difficult.)

Second, in the current deferred implementation, a message of cancellation can not propagate to chained deferred objects because the chain is built by the singly linked list. Here is an example:

(deferred:cancel
  (deferred:$
    (deferred:wait 1000) ; timeout msec
    (deferred:nextc it (lambda () (message "canceled!1")))
    (deferred:nextc it (lambda () (message "canceled!2")))))

This code shows only the message "canceled!1". The last deferred object is canceled, but other two objects are not.

At the current spec of JavaScript Promise in ES6, the promise does not have cancellation and Promise.race also ignores the rest results. (Some implementations has cancellation, such as bluebird and winjs.promise.)

If I have some time to improve deferred.el code, I would like to implement correct cancellation function...

kiwanami commented 10 years ago

I Updated the document, 1654b7c7fec8fd3e4088596e6525d8a9d0a0253e.