plz.el icon indicating copy to clipboard operation
plz.el copied to clipboard

is it possible to build a data structure form multiple async requests?

Open titaniumbones opened this issue 4 years ago • 1 comments

I am making many sequential async requests, each of which returns some JSON. I'd like to build a list of all these responses to use later on. Your async example shows a :then which issues a message, but is there any way to capture the result of all the requests? My library uses lexical binding, so I am pretty sure I can't just do this:

(defun collect-results` (urls) 
(let ((collection '()))
(-map (lambda (url) 
(plz 'post url
  :headers '(("Content-Type" . "application/json"))
  :body (json-encode '(("key" . "value")))
  :as #'json-read
  :then (lambda (alist)
          (append (list (alist-get 'data alist))) collection))))

Is there currently any possibility of doing this?

titaniumbones avatar Nov 24 '21 13:11 titaniumbones

Hi Matt,

I'm not sure exactly what you mean, but AFAICT, yes. Please see the test file, which uses lexical binding to do that sort of thing. In your code example, you should just need to use a defvar for the variable rather than binding it locally in the function. OTOH, if you don't want to use a defvar, then maybe you don't need to use async requests, especially if they are really sequential.

alphapapa avatar Nov 27 '21 18:11 alphapapa

By the way, in the code you posted, you would need to use setq to actually change the value of the collection variable.

alphapapa avatar Dec 31 '22 22:12 alphapapa