I'm trying to use drakma-async in my small project. But I just can't understand what's happening. (I use emacs + slime + ccl). I need to get data with http(s) and parse it in a callback. I assume I can get wrong data that cannot be parsed, so I want to make a retry. But when I tried to make some tests I just can't understand what's happening...
(defun my-callback (data)
(prin1 data)
(restart-case
(error "Some error parsing data...")
(just-continue () (prin1 "Continue..."))))
(defun simple-test ()
(let ((future (asf:make-future)))
(as:delay #'(lambda () (asf:finish future "Some data")) :time 2)
(prin1 (asf:future-finished-p future))
(asf:attach future #'my-callback)))
(defun drakma-test ()
(asf:alet ((response (das:http-request "http://www.google.com")))
;(prin1 (asf:future-finished-p response))
(asf:attach response #'my-callback)))
(defun drakma-test-let ()
(let ((response (das:http-request "http://www.google.com")))
;(prin1 (asf:future-finished-p response))
(asf:attach response #'my-callback)))
(defun run-test (test)
(as:start-event-loop test))
1) So I will that's what I have with my simple example (that's what I've planned)
? (run-test #'simple-test)
NIL"Some data" ;I get debugger here with simple-error and choose my restart
Invoking restart: #<RESTART JUST-CONTINUE #x7F0578EC20AD>
"Continue..."
1
2) Here what I get in second test:
? (run-test #'drakma-test)
"<A LOT OF HTML>
"
1
Where are my debugger and my restart?
3) Uncomment the ;(prin1 (asf:future...)) line in drakma-test
? (run-test #'drakma-test)
1
No finished/unfinished bool, No Data is not printed, I don't get a restart, I just get 1 as result.
4) I assume if i write (let ((reponse (das:http-request "http://www.google.com"))) ... )
instad of (asf:alet ...) the response will contain not future object, but will block until the request will be finished and the response will contain the data.
? (run-test #'drakma-test-let)
1
5) Uncomment the ;(prin1 (asf:future...)) line in drakma-test-let
? (run-test #'drakma-test-let)
NIL ;future is not finished
1
Data is not printed, just that is not finished and the result of run-test.
I've run tests for cl-async and they all passed except the ipv6 test. So I just don't know where to start to understand whats happening... Why I get no debugger and restart in 2nd test? Why nothing happens in 3rd test (it's the same as 2nd, but with prin1). Why nothing happens in 5th and 5th tests?
P.S. Don't have enough reputation to create drakma-async or cl-async tags for this libraries. I know that drakma-async is built over drakma so I put this tag.
Thanks for m-n's comment that made the situation clearer and explained shortly the situation.
I made some examples and want to show what happens in each case:
Example:
And here is what's happening:
A)
asf:aletwait for result and bind the result value to the variable. So I was wrong thinking thatasf:aletbind a future.B) In
make-example-futurewe wrapasf:finishwithasf:future-handler-caseand useasf:signal-errorto send error to future. That means that error is handled and theerrbackwill be called. Even if the callback is attached later in the code. Moreover, the error with(asf:future-finished-p result)was handled withfuture-handler-casebecause it was wrapped inasf:alet(At least I think so).C) Comment the
(asf:future-finished-p result)and the result isIn
drakma-asyncthere is similarfuture-handler-casewrapper that wrapsasf:finish.So this explains the
#2test result. I got the data andasf:aletreturned me the string. The error from callback was passed toerrback, which I didn't have. Moreover. Indrakma-testusing onlyasf:aletI just can't attacherrbackbecause I don't have access to future. I need to callhttp-requestinlet, not inalet.Also this explains the result of the
#3test: I got error in(future-finished-p)which was sent toerrback.If we look at the result of
#4and#5test with newmy-callback: It can be seen, thatcl-asynctry to call my callback with all values thedrakmareturned. There are 7 of them (the values thatdrakma:http-requestreturn). So I tried to attach wrong number of arguments callback and my #4 and #5 tests were signalling an error that was simply handled by thatfuture-hander-caseand send it toerrback.Result: Anyway, it seems impossible to use restarts with
drakma-asyncwithout removing thatfuture-handler-casebecause it send error to errback, but lose all restarts.Hope this post helps if somebody fill face up with my question.