<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Clojure Q&amp;A - Recent questions tagged promise</title>
<link>https://ask.clojure.org/index.php/tag/promise</link>
<description></description>
<item>
<title>Future doesn't stop running</title>
<link>https://ask.clojure.org/index.php/12637/future-doesnt-stop-running</link>
<description>&lt;pre&gt;&lt;code&gt;(defn get-result-from-first-engine
&quot;Performs search on Powells and Cultura and returns the results
which comes first&quot;
[query]
(let [result (promise)]
    (doseq [engine [&quot;powells&quot; &quot;cultura&quot;]]
        (future (if-let [engine-result (search query engine)]
            (deliver result engine-result)
        ))
    )
    (println &quot;First result: &quot; (deref result 2000 &quot;timed out&quot;))
))

(defn -main
    [&amp;amp; args]

    (time (do
        (println (search &quot;clojure&quot; &quot;powells&quot;))
        (println (search &quot;clojure&quot; &quot;cultura&quot;))
    ))

    (time (get-result-from-first-engine &quot;clojure&quot;))
)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This code is part of my solution to the first exercise from chapter 9 of &quot;Clojure for the Brave and True&quot;. I want to perform the two requests in parallel and get the response of the one who process first.&lt;/p&gt;
&lt;p&gt;Two weird things are happening:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The time from the parallel is WAY lower than the sequential one, written in main. Result example below. The time difference is so discrepant, that i guess if clojure caches it someway. I didn't use memoize or anything like that.&lt;/li&gt;
&lt;li&gt;The main doesn't stop running. It holds like if it was waiting for an input or processing something. My theory is that the future doesn't stop running, but i don't know why&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Time difference between parallel and sequential:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;https://www.powells.com/book/clojure-cookbook-9781449366179
https://www3.livrariacultura.com.br/clojure-94991222/p
&quot;Elapsed time: 2185.2716 msecs&quot;
First result:  https://www3.livrariacultura.com.br/clojure-94991222/p
&quot;Elapsed time: 63.5558 msecs&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Thanks for the help in advance&lt;/p&gt;
</description>
<category>Clojure</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12637/future-doesnt-stop-running</guid>
<pubDate>Fri, 03 Feb 2023 15:32:19 +0000</pubDate>
</item>
<item>
<title>Why is reified clojure.lang.IDeref wrapping a promise blocking?</title>
<link>https://ask.clojure.org/index.php/12061/why-reified-clojure-lang-ideref-wrapping-promise-blocking</link>
<description>&lt;p&gt;Using clojure 1.10.3:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;user&amp;gt; (def p (promise))
#'user/p
user&amp;gt; (def wrapper
        (reify
          clojure.lang.IDeref
          (deref [_] @p)))
#'user/wrapper
user&amp;gt; wrapper ;; =&amp;gt; repl hangs here
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In above session the repl blocks on the last statement. I do not see why that is. I would expect &lt;code&gt;@wrapper&lt;/code&gt; to block, evaluating &lt;code&gt;wrapper&lt;/code&gt; however I expected it to return an Object of some kind. Am I missing something here?&lt;/p&gt;
</description>
<category>Clojure</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12061/why-reified-clojure-lang-ideref-wrapping-promise-blocking</guid>
<pubDate>Thu, 21 Jul 2022 20:01:55 +0000</pubDate>
</item>
</channel>
</rss>