metosin / sieppari

Small, fast, and complete interceptor library for Clojure/Script
Eclipse Public License 2.0
207 stars 21 forks source link

[CLJS] Async :leave not working #19

Closed lilactown closed 5 years ago

lilactown commented 5 years ago

I've only tested this in CLJS with native promises. Currently, an async value returned by the :leave function on an interceptor is not awaited.

Example:

(sieppari/execute
 [{:enter (fn inc-enter [ctx]
            (js/Promise.resolve (update-in ctx [:request :x] inc)))
   :leave (fn inc-leave [ctx]
            (js/Promise.resolve (update-in ctx [:request :x] inc)))}
  (fn handler [request] {:y (inc (:x request))})]
 {:x 40}
 prn)

Result: Prints 42

Expected result: Prints 43; 40 + 1 (for enter) + 1 (for leave) + 1 (for handler)

I have a case where I'm trying to do some HTTP request using js/fetch and then coerce the body. js/fetch returns a Promise fulfilled with a Response type, which you then call .json / .text / etc. These methods return a new promise that must be awaited.

Please advise! Is this expected behavior?

lilactown commented 5 years ago

I understand my mistake: :leave should operate on the :response key in the ctx, not the :request key. This was not obvious to me even though the examples are written that way. 😵