Fork me on GitHub

Hi, what it the right approach to test async code with clojure? I’m not using core.async, I just have a small function which calls a callback at the end. Is there anything for that? Didn’t find anything in clojure.test


@artemyarulin: I'd say you just need to arrange your test fn in such a way so it waits for the async behavior to finish so you can do assertions about the result or about side effects. Or you could have assertions intermingled with your async code, that's fine too, again you just need to wait for that to execute


or do you think something else is needed?


@nberger: Thank you. Hm, I know how can I block with core.async for example, but don’t want to bring it only for those thing. Assuming I have a simple function (fn[{:keys [a]} cb] (cb {:b a})) how can I test it?


hmm ok, I see... maybe you want a latch so you know when to finish?


@artemyarulin: you can make the callback deliver a promise and block the test dereferencing the promise, see


not sure what latch means here, but @dialelo idea of using promise is a good one - no need to bring any dependency just for this small test. Thank you both in any case!


you're welcome!


there are a lot of goodies in core so usually I try to search there first


yeap, I came from CLJS where we have, was looking for the same thing at first


I like the promise way too. The latch is not an additional dep, but I'm now seeing it's used in core.async cljs tests, not in clj tests. So yeah, promise is better 👍


Thank you in any case simple_smile