Skip to content

A very simple ClojureScript library for working with promises and callbacks via core.async.

Notifications You must be signed in to change notification settings

potapenko/cljs-await

Folders and files

NameName
Last commit message
Last commit date

Latest commit

e8d6025 · Apr 24, 2019

History

14 Commits
Jan 13, 2018
Jan 13, 2018
Jan 13, 2018
Apr 24, 2019
Jan 13, 2018
Jul 15, 2018
Jul 15, 2018

Repository files navigation

cljs-await

[cljs-await "1.0.2"]

A very simple ClojureScript library for working with promises and callbacks via core.async.

await

Works with JS promises. Returns [err res] where err - a caught exception (reject) and res - the result of the promise work (resolve).

(ns my-ns
  (:require [cljs-await.core :refer [await]]
             [cljs.core.async :as async :refer [<! >! put! chan timeout]])
            (:require-macros [cljs.core.async.macros :refer [go go-loop]]))

  (defn- ->promise [t]
    (js/Promise. (fn [resolve reject] (js/setTimeout resolve, t, "hello from promise!"))))

  (go
    (let [[err res] (<! (await (->promise 2000)))]
      (println res)))

await-cb

Works with functions, the last argument is the function callback. Returns [err res]. Where err - an exception (if it happens) and res - the result of the work of the callback function (nil - if there was an exception).

  (ns my-ns
    (:require [cljs-await.core :refer [await-cb]]
              [cljs.core.async :as async :refer [<! >! put! chan timeout]])
    (:require-macros [cljs.core.async.macros :refer [go go-loop]]))
  
  (defn- ->callback []
    (fn [cb] (cb "hello from callback!")))

  (go
    (let [[err res] (<! (await-cb (->callback)))]
      (println res)))

License

License

Copyright © 2017 Eugene Potapenko

Distributed under the Eclipse Public License, the same as Clojure.

About

A very simple ClojureScript library for working with promises and callbacks via core.async.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published