Fork me on GitHub
#clojurescript
<
2023-04-10
>
jmckitrick12:04:50

Isn’t there a trick or caveat to handling exceptions in cljs go blocks? I seem to recall they can be silently swallowed if it isn’t done right.

jmckitrick13:04:30

And once I’ve used <p! inside a go block to get the value of a function that returns a Promise, how can I access that value returned from the go block? It doesn’t look like cljs has a <!! macro for use outside go blocks.

p-himik13:04:23

That's correct. go returns a channel and you must either pass that channel around till you eventually get a value from it in another go block or simply remain in the same go block. Same deal with promises. You can either keep on chaining .then right there where you got the promise or you can pass it somewhere else and call .then there. You can't just get the value.

p-himik13:04:01

Oh, there's also take! - you can pass a callback to it.

p-himik13:04:45

And poll! if you're in a hurry and are fine with the potential lack of a result.

jmckitrick13:04:47

Hmm. So if the function returns a promise and I need to return that value, what’s the best way to do it? Or I should say if I need to use that value?

jmckitrick13:04:36

I discovered take! About an hour ago, so I’m going to experiment with it but so far it’s not returning the value I expect or I should say it’s not logging to the console the value that I expect

p-himik13:04:59

You can't return the value. You can make the promise deliver the value to some other function. And just in case - if all you're using core.async for is getting values from promises, then don't. Just use promises, without core.async - much simpler that way.

jmckitrick13:04:12

I guess I could do some thing like setting an atom and then waiting for it to be populated but that seems really kludgy

jmckitrick13:04:36

I think you’re right I just need to stick with chaining the promises as usual

p-himik13:04:37

Nothing in the JS world that's async will simply return a value. It will always return a promise or something similar. Even the JS's await is just sugar around promises.

jmckitrick13:04:05

I used to have fun with this in Scala and it always drove me crazy lol. Flat map everything.

jmckitrick13:04:49

Scala futures

jmckitrick13:04:04

I think take! would do the trick, but I’ll stick with chaining. Good suggestion.

👍 2
jmckitrick14:04:56

Yeah that’s what I’ll do. Basically a call back.

jmckitrick14:04:55

But that was a really good explanation, thanks! @U90R0EPHA

☺️ 1
alpox17:04:33

If you like some simple helpers for promises there is also https://github.com/funcool/promesa I enjoy it a lot. Especially p/let

jmckitrick17:04:52

I’ve heard good things about it as well