So, core.async draws a lot of inspiration from Go (the programming language).
It is a more flexible model for async programming than a simple async/await as
you find in C#.
You can emulate C# async/await by thinking of channels as tasks (every function
that returns a task in C# is awaitable).
(defn async-get [url]
(let [c (channel)]
(ajax/get url {:when-done #(>!! c %)}) ;; >!! puts something into a channel
outside of a go-block
c))
(go
(<! (async-get "some/url"))) ;; <! receives a value inside of a go-block,
just as await does inside an async function
The code above would be the exact same in C#, except in C# you have async
functions in the standard library, so you rarely need to make your own
"async-get" function. Channels do become more intereseting in combination with
alt and timeout.
(go (alt!
(async-get "some/url") ([val] val)
(timeout 1000) ([_] "failed")))
The above code will return "failed" unless our async-get call completes within
a second, if that happens it will return the result of async-get. We can have
many more channels within our alt! call, and only one value is exposed from one
channel.
In the end, core.async is about making async code easy to reason about. Use it
as async/await if you wish, or use it like in Go.
--
Note that posts from new members are moderated - please be patient with your
first post.
---
You received this message because you are subscribed to the Google Groups
"ClojureScript" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/clojurescript.