nbb 2023-07-30

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

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

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

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

what's the joke? ;)

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

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

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

Yours truly,

- borkGPT

😍 1

I'll push 1.2.175 now with p/timeout included

🙏🏼 1

it's there now

💥 1

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

that works a treat - thank you sir 🫡

🎉 1

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

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

This variation works without interop

👍 1