Fork me on GitHub
#nbb
<
2023-07-30
>
ray14:07:31

hitting a problem with timeout on promesa ... seems like it's missing?

ray14:07:22

Welcome to nbb v1.2.174!
user=> (require '[promesa.core :as p])
nil
user=> (-> (some-async-task)
    (p/timeout 200)
    (p/then #(println "Task finished" %))
    (p/catch #(println "Timeout" %)))
"#error {:message \"Could not resolve symbol: p/timeout\", :data {:type :sci/error, :line 2, :column 5, :message \"Could not resolve symbol: p/timeout\", :sci.impl/callstack #object[cljs.core.Volatile {:val ({:line 1, :column 1, :ns #object[Ur user], :file nil} {:line 1, :column 1, :ns #object[Ur user], :file nil, :sci.impl/f-meta {:ns #object[Ur clojure.core], :macro true, :sci/built-in true, :name ->, :arglists ([x & forms]), :doc \"Threads the expr through the forms. Inserts x as the\\n  second item in the first form, making a list of it if it is not a\\n  list already. If there are more forms, inserts the first form as the\\n  second item in second form, etc.\"}} {:line 4, :column 5, :ns #object[Ur user], :file nil} {:line 4, :column 5, :ns #object[Ur user], :file nil, :sci.impl/f-meta {:ns #object[Ur promesa.core], :name catch, :arglists ([p f] [p pred-or-type f]), :doc \"Executes `f` when the promise `p` is rejected. Returns a promise\\n  resolved with the return value of `f` function handler.\"}} {:line 3, :column 5, :ns #object[Ur user], :file nil} {:line 3, :column 5, :ns #object[Ur user], :file nil, :sci.impl/f-meta {:ns #object[Ur promesa.core], :name then, :arglists ([p f] [p f executor]), :doc \"Chains a computation `f` (function) to be executed when the promise\\n  `p` is successfully resolved.\\n\\n  The computation will be executed in the calling thread by default;\\n  you also can provide a custom executor.\\n\\n  If the function `f` returns a promise instance, it will be\\n  automatically unwrapped.\"}} {:line 2, :column 5, :ns #object[Ur user], :file nil})}], :file nil, :phase \"analysis\"}, :cause #error {:message \"Could not resolve symbol: p/timeout\", :data {:type :sci/error, :line 4, :column 5, :file nil, :phase \"analysis\"}}}"
user=> 

ray14:07:34

p/delay does work but the contract is different ie it always waits the full amount of time

borkdude14:07:40

I think it needs to be added to sci.configs. I'll add it but for now you can use:

(js/Promise.race promise1 (p/delay 1000))

😂 2
borkdude15:07:25

what's the joke? ;)

ray15:07:49

You're my new ChatBorkPT

Welcome to nbb v1.2.174!
user=> (require '[promesa.core :as p])
nil
user=> (let [results (p/deferred)]
  (p/let [answer (js/Promise.race results (p/delay 1000))]
    answer))
#<Promise[~]>
user=> object is not iterable (cannot read property Symbol(Symbol.iterator))

ray15:07:38

I think it's

ray15:07:46

(let [results (p/deferred)]
  (p/let [answer (js/Promise.race [results (p/delay 1000)])]
    answer))

borkdude15:07:11

ah right. if you have another question, please share it!

Yours truly,

- borkGPT

😍 2
borkdude15:07:46

I'll push 1.2.175 now with p/timeout included

2
borkdude15:07:16

it's there now

💥 2
ray15:07:04

incidentally ... and I think you knew it but just to nail it ... the above library API is a mix of sync / async and the use of a deferred promise is just a trick to ensure that the connection is made

(defn ->consumer
  [options & {:keys [data-handler] :or {data-handler kv-data}}]
  (let [consumer (KafkaConsumer. options)
        connection (p/deferred)]            ;; <<<------- ensure we return a connected consumer
    (.on consumer "ready" (fn [] (p/resolve! connection consumer)))
    (.on consumer "data" data-handler)
    (.on consumer "event.error" (fn [err] (p/reject! connection err)))
    (.connect consumer)
    connection))

ray15:07:32

that works a treat - thank you sir :saluting_face:

🎉 2
Chris McCormick03:07:54

Wow that timeout trick is going to save me a lot of faffing about in future, thanks!

ray06:07:44

(let [results (p/deferred)]
  (p/let [answer (p/any [results (p/delay 1000)])]
    answer))

ray06:07:21

This variation works without interop

👍 2