Fork me on GitHub
#core-async
<
2021-07-29
>
Quentin Le Guennec20:07:04

Hello, anyone knows why this returns nil?

(let [c (a/chan)]
  (a/>!! c "Blocking - not in go-block")
  (a/<!! c))

phronmophobic20:07:50

c is an unbuffered channel and it will forever block at (a/>!! c "Blocking - not in go-block") since it's impossible for anyone else to read from the channel

phronmophobic20:07:13

I'm not sure what makes it look like it's returning nil. Maybe your repl is timing out and just returning nil?

phronmophobic20:07:03

changing the buffer size to 1 will return "Blocking - not in go-block"

(let [c (a/chan 1)]
  (a/>!! c "Blocking - not in go-block")
  (a/<!! c))
;; "Blocking - not in go-block"

Quentin Le Guennec20:07:42

It actually seems like it's not the issue. Your snippet also returns nil.

phronmophobic20:07:23

not in my repl

Quentin Le Guennec20:07:25

I'm on clojurescript though, and I've got an error in the repl which says that >!! is undeclared

phronmophobic20:07:45

blocking operations are not supported in clojurescript

👍 3
Quentin Le Guennec20:07:43

I'm not entirely sure how I'm supposed to take the value out of a go block then?

phronmophobic20:07:36

I'm less familiar with cljs core.async at this point, but there at least 3 options: • take!poll! • wrap your <! in another go block

Joshua Suskalo20:07:00

You don't really. One way to communicate some value out of a go block would be to modify an atom, but because of how JS works once you're in async code (like a go block) there isn't really a good way out of it.

phronmophobic20:07:19

I think I remember seeing some integration with promises/await, but I don't have link handy at the moment

Joshua Suskalo20:07:40

right, but await et al are still just right back in async code since you can only await in an async function.

Joshua Suskalo20:07:57

unless I'm remembering my js wrong.

Quentin Le Guennec21:07:08

I can only pass values around, but not evaluate some async code in the repl and get a result

Quentin Le Guennec21:07:26

That makes sense since browser js is single threaded

👍 3
phronmophobic21:07:30

> right, but await et al are still just right back in async code It is core.async after all

phronmophobic21:07:38

even on the jvm, <!! is blocking, but it's still asynchronous. It's just a matter of whether you want to use the OS scheduler or the scheduler that core.async's go macro uses (there are some other resource differences too).