I'm using emacs-request to get some json data from the web. Here's an example
(defun test (arg1 arg2)
(request
"http://httpbin.org/get"
:params '(("key" . "value") ("key2" . "value2"))
:parser 'json-read
:success (cl-function
(lambda (&key data &allow-other-keys)
(message "I sent: %S" (assoc-default 'args data))))))
I'm wondering how the callback functions such as :success
can have access to arg1 and arg2?
You can either set the lexical-binding
variable to t
, allowing the lambda to have access to the outer function's arguments, or wrap the :success
function in a lexical-let
that binds the outer function's arguments for the lambda:
(defun test (arg1 arg2)
(request
"http://httpbin.org/get"
:params '(("key" . "value") ("key2" . "value2"))
:parser 'json-read
:success (lexical-let ((arg1 arg1) (arg2 arg2))
(cl-function
(lambda (&key data &allow-other-keys)
(message "%s %s sent: %S" arg1 arg2 (assoc-default 'args data)))))))