Skip to content

Commit

Permalink
Update example code on README
Browse files Browse the repository at this point in the history
  • Loading branch information
athos committed Nov 1, 2019
1 parent b8dcef8 commit ba8003a
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,34 @@ kitchen-async focuses on the ease of Promise handling, and is not specifically i
Assume you are writing some `Promise`-heavy async code in ClojureScript (e.g. [Google's Puppeteer](https://github.com/GoogleChrome/puppeteer) provides such a collection of APIs). Then, if you only use raw JavaScript interop facilities for it, you would have to write something like this:

```clj
(-> (puppeteer/launch)
(def puppeteer (js/require "puppeteer"))

(-> (.launch puppeteer)
(.then (fn [browser]
(-> (.newPage browser)
(.then (fn [page]
(.then (.goto page "https://www.google.com")
#(.screenshot page #js{:path "screenshot.png"}))))
(.then #(.close browser))))))
(-> (.goto page "https://clojure.org")
(.then #(.screenshot page #js{:path "screenshot.png"}))
(.catch js/console.error)
(.then #(.close browser)))))))))
```

`kitchen-async` provides more succinct, "direct style" syntactic sugar for those things, which you may find similar to `async/await` in ECMAScript 2017:

```clj
(require '[kitchen-async.promise :as p])

(p/let [browser (puppeteer/launch)
(def puppeteer (js/require "puppeteer"))

(p/let [browser (.launch puppeteer)
page (.newPage browser)]
(.goto page "https://www.google.com")
(.screenshot page #js{:path "screenshot.png"})
(.close browser))
(p/try
(.goto page "https://clojure.org")
(.screenshot page #js{:path "screenshot.png"})
(p/catch :default e
(js/console.error e))
(p/finally
(.close browser))))
```

## Installation
Expand Down

0 comments on commit ba8003a

Please sign in to comment.