core-async

RAJKUMAR 2025-03-10T22:00:27.363049Z

what is wrong with my async code

RAJKUMAR 2025-03-10T22:00:33.766469Z

(let [ch (async/chan)
      result (do
        (println "inside do")
        (async/timeout 50)
        (async/>!! ch "bar")
        ch)]
(println (async/

RAJKUMAR 2025-03-10T22:00:56.947289Z

it only prints "inside do" but not bar

RAJKUMAR 2025-03-10T22:01:16.804689Z

If I increase the channel size to 2 then it prints "bar"

phronmophobic 2025-03-10T22:04:17.043039Z

Writing to ch will block until someone reads from the channel since it's unbuffered. Adding a buffer of 2 means that it can hold two values before blocking.

phronmophobic 2025-03-10T22:04:50.252509Z

also calling (async/timeout 50) won't do anything. You probably meant (async/<!! (async/timeout 50)).

phronmophobic 2025-03-10T22:05:10.650339Z

async/timeout returns a channel.

phronmophobic 2025-03-10T22:05:45.237989Z

(async/chan) returns an unbuffered channel. Try (async/chan 1) to see the difference.

RAJKUMAR 2025-03-10T22:06:12.289879Z

Thanks @smith.adriane

2025-03-10T23:41:20.139609Z

Your code doesn't start any new threads, so you have a thread attempting to write to a channel with no one waiting to read from the channel, so your thread just blocks

2025-03-11T16:27:23.875159Z

You should think of this like a tag team wrestling match. When you read/write to a channel, you're tagging out and tagging in another process.