nbb

ray 2023-07-30T14:28:31.949379Z

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

ray 2023-07-30T14:29:22.637889Z

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=> 

ray 2023-07-30T14:31:34.026489Z

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

borkdude 2023-07-30T14:51:40.915939Z

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))

😂 1
ray 2023-07-30T15:04:41.553599Z

ok

borkdude 2023-07-30T15:05:25.650729Z

what's the joke? ;)

ray 2023-07-30T15:10:49.040739Z

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))

ray 2023-07-30T15:11:38.471239Z

I think it's

ray 2023-07-30T15:11:46.584599Z

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

borkdude 2023-07-30T15:12:11.980379Z

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

Yours truly,

- borkGPT

😍 1
borkdude 2023-07-30T15:12:29.577319Z

😂

borkdude 2023-07-30T15:13:46.687149Z

I'll push 1.2.175 now with p/timeout included

🙏🏼 1
borkdude 2023-07-30T15:18:16.152349Z

it's there now

💥 1
ray 2023-07-30T15:27:04.095029Z

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))

borkdude 2023-07-30T15:28:44.988269Z

nice

ray 2023-07-30T15:33:32.960829Z

that works a treat - thank you sir 🫡

🎉 1
Chris McCormick 2023-07-31T03:16:54.040999Z

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

ray 2023-07-31T06:27:44.077759Z

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

ray 2023-07-31T06:28:21.059699Z

This variation works without interop

👍 1