Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jamesmacaulay/cljs-promises

A ClojureScript library for using JS promises with core.async
https://github.com/jamesmacaulay/cljs-promises

Last synced: 24 days ago
JSON representation

A ClojureScript library for using JS promises with core.async

Awesome Lists containing this project

README

        

# cljs-promises

A ClojureScript library for working with JavaScript promises.

### Overview

This library leverages the power of [core.async](https://github.com/clojure/core.async) to let you write promise code like this:

```clojure
(cljs-promises.async/extend-promises-as-pair-channels!)

(go
(let [user-promise (get-user "jamesmacaulay")]
(try
(println (:blog ( user-promise)))
(catch js/Error e
(println (str "Could't get user data: " (ex-message e))))))
```

The first line globally extends `Promise` instances to act like **read-only channels** which, once resolved, **endlessly produce the same value or error to anyone who takes from them**. Because promises have built-in error semantics which don't have any direct corollary in core.async, there are different ways that we can represent promise results. In this example we've extended promises in a way that values taken from them with core.async are actually `[value error]` pairs, where you get `[value nil]` from a fulfilled promise and `[nil error]` from a rejected promise.

`` is a macro provided by `cljs-promises.async` which takes one of these pairs from a promise with core.async's `