ちゃんと休まないと治らないものも治らない…
最初のawait ...の評価で#
が流れてくるのがなぜか分からん
(require racket/control)
(define (then promise chain)
(lambda (resolve reject)
(promise (lambda (x) (resolve (chain x))) reject)))
(define (catch promise chain)
(lambda (resolve reject)
(promise resolve (lambda (x) (reject (chain x))))))
(define-syntax async
(syntax-rules ()
((_ await body ...)
(let ((tag (make-continuation-prompt-tag)))
(lambda (resolve reject)
(resolve
(let/cc break
(commit-loop reject
(reset-at tag
(let ((await (lambda (promise) (shift-at tag k (then promise k)))))
(break (begin body ...))))))))))))
(define-syntax define-async
(syntax-rules ()
((_ await (proc . args) body ...) (define (proc . args) (async await body ...)))))
(define (commit-loop reject promise)
(promise
(lambda (result) (commit-loop reject result))
(lambda (reason) (reject reason))))
(define (commit promise)
(promise print print))
(define fetch-queue '())
(define (fetch-promise resolve reject)
(set! fetch-queue
(cons
(lambda (x) (print "fetched") (resolve x))
fetch-queue)))
(define (onFetch data)
((last fetch-queue) data)
(set! fetch-queue (drop-right fetch-queue 1)))
(define-async await (fetch-n n)
(let loop ((m 0))
(if (= n m) n
(begin
(print
(format "fetch #~a: ~a" m (await fetch-promise)))
(loop (+ m 1))))))
> (commit (async await (print (await (fetch-n 3)))))
> (onFetch "test")
> (onFetch "test2")
> (onFetch "test3")
(set! fetch-queue ...)
の返り値がcommit-loop
の返り値になってそれがbreakして流れてくるのだった
commit-loopは普通の意味でのループではないことを忘れていた
RE: https://misskey-square.net/notes/9z69mlt8gn
継続ひねくり回していると、内部状態を正確にイメージできないから怪しそうなところを勘でプログラムして偶然動いたらラッキーという誤った方策に走りがちな初心者の気持ちがわかる