[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
How to use a variable from outer scope in the success function of a requ
From: |
Iñigo Serna |
Subject: |
How to use a variable from outer scope in the success function of a request call? |
Date: |
Sat, 29 Aug 2015 02:36:27 +0200 |
Hi,
; I can't use a variable inside the success lambda function of a web request
call.
; As this is difficult to explain I'll show some things I've tried.
(require 'request)
(fnX "TEXT") ; this is the function call.
; In `fn1' execution fails because the c variable in the message calls is not
found inside the success function scope.
(defun fn1 (c)
(request "https://api.stackexchange.com/2.1/questions"
:params '((order . "desc")
(sort . "activity")
(site . "stackoverflow"))
:parser 'json-read
:success (function*
(lambda (&key data &allow-other-keys)
(let* ((item (elt (assoc-default 'items data) 0))
(title (assoc-default 'title item))
(tags (assoc-default 'tags item)))
(message "PARAMS: %s" c)
(message "=> %s %S" title tags)
(message "LAST: %s" c))))))
; In `fn2' I've added c as parameter for lambda function, but I don't know how
to pass the external c variable to the lambda function. Now it doesn't fail,
but it shows nil.
(defun fn2 (c)
(request "https://api.stackexchange.com/2.1/questions"
:params '((order . "desc")
(sort . "activity")
(site . "stackoverflow"))
:parser 'json-read
:success (function*
(lambda (&key data c &allow-other-keys)
(let* ((item (elt (assoc-default 'items data) 0))
(title (assoc-default 'title item))
(tags (assoc-default 'tags item)))
(message "PARAMS: %s" c)
(message "=> %s %S" title tags)
(message "LAST: %s" c))))))
; So I'm trying to use defmacros to pass the variable.
(defmacro my-request2 (url func &optional more)
"Call REQUEST URL and execute FUNC on success."
`(request ,url
:params '((order . "desc")
(sort . "activity")
(site . "stackoverflow"))
:parser 'json-read
:success (function* (lambda (&key data &allow-other-keys) (funcall
,func data ,more)))))
; Here, `fn3' does not crash but it shows c as symbol 'c', not the contents of
c variable.
(defun fn3 (c)
(my-request2 "https://api.stackexchange.com/2.1/questions"
#'(lambda (data &optional d)
(let* ((item (elt (assoc-default 'items data) 0))
(title (assoc-default 'title item))
(tags (assoc-default 'tags item)))
(message "PARAMS: %S" d)
(message "=> %s %S" title tags)
(message "LAST: %S" (nth 2 d))))
'("OOOO" "AAAA" c)))
; `fn4' directly fails, and don't know why. I expect this alternative to work.
(defun fn4 (c)
(my-request2 "https://api.stackexchange.com/2.1/questions"
#'(lambda (data &optional d)
(let* ((item (elt (assoc-default 'items data) 0))
(title (assoc-default 'title item))
(tags (assoc-default 'tags item)))
(message "PARAMS: %S" d)
(message "=> %s %S" title tags)
(message "LAST: %S" (nth 2 d))))
(list "OOOO" "AAAA" c)))
; Finally, `fn5' works, but this adds a second defmacro
(defmacro fn5 (c)
`(my-request2 "https://api.stackexchange.com/2.1/questions"
#'(lambda (data &optional d)
(let* ((item (elt (assoc-default 'items data) 0))
(title (assoc-default 'title item))
(tags (assoc-default 'tags item)))
(message "PARAMS: %S" d)
(message "=> %s %S" title tags)
(message "LAST: %S" (nth 2 d))))
'("OOOO" "AAAA" ,c)))
(fn5 "TEXT")
; Is there any simpler way I'm missing? Why `fn4' does not work?
Thanks,
--
Iñigo Serna
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- How to use a variable from outer scope in the success function of a request call?,
Iñigo Serna <=