emacs-deferred icon indicating copy to clipboard operation
emacs-deferred copied to clipboard

Correct way of making parallel http post requests

Open bizzyman opened this issue 7 years ago • 0 comments

Hi, I am trying to make a number of concurrent http post requests from inside emacs. The deferred:parallel function seems perfect for this task however I cannot get it to work.

I have an example function with three requests being made bellow. I would like them to all complete asynchronously in parallel and then return the result as a list in the deferred:nextc function so I can operate on them.

(defun my-deferred-func ()
  (let ((lk ))
   (deferred:$
     (deferred:parallel
       (request (format "http://localhost:%s" tern-known-port)
                :type "POST"
                :parser 'json-read
                :success (function* (lambda (&key data &allow-other-keys)
                                      (push  (cdr (car data)) lk)))
                :data
                (format "{\"query\":{\"end\":%s,\"file\":\"%s\",\"type\":\"type\",\"preferFunction\":true}}"
                        196
                        (buffer-file-name)
                        )
                :sync nil)
       (request (format "http://localhost:%s" tern-known-port)
                :type "POST"
                :parser 'json-read
                :success (function* (lambda (&key data &allow-other-keys)
                                      (push  (cdr (car data)) lk)))
                :data
                (format "{\"query\":{\"end\":%s,\"file\":\"%s\",\"type\":\"type\",\"preferFunction\":true}}"
                        196
                        (buffer-file-name)
                        )
                :sync nil)
       (request (format "http://localhost:%s" tern-known-port)
                :type "POST"
                :parser 'json-read
                :success (function* (lambda (&key data &allow-other-keys)
                                      (push  (cdr (car data)) lk)))
                :data
                (format "{\"query\":{\"end\":%s,\"file\":\"%s\",\"type\":\"type\",\"preferFunction\":true}}"
                        196
                        (buffer-file-name)
                        )
                :sync nil)
       )
     (deferred:nextc it (lambda (args) args))
     )))

The above requests make a simple post request to the ternjs server.

Any guidance on using deferred:parallel? I have also seen the deferred:url-post but having trouble wrapping my head around that function too.

bizzyman avatar Jan 24 '18 17:01 bizzyman