Skip to content

Instantly share code, notes, and snippets.

@pesterhazy
pesterhazy / promises-cljs.md
Last active October 10, 2023 18:09
Promises in ClojureScript

Chaining promises

Chaining promises in ClojureScript is best done using the thread-first macro, ->. Here's an example of using the fetch API:

(-> (js/fetch "/data")
    (.then (fn [r]
             (when-not (.-ok r)
               (throw (js/Error. "Could not fetch /data")))
             (.json r)))
@vitalipe
vitalipe / get-in.js
Created October 11, 2016 01:13
get-in form clojure in JS
// returns the value in a nested associative structure,
// just like (get-in) in clojure
var getIn = function (obj, path) {
if (!_(path).isArray()) // normalize
path = _(arguments).toArray().rest().value();
if (path.length === 0)
return obj;
if (!_(obj).has(_.first(path)))
@Chouser
Chouser / externs_for_cljs.clj
Created June 17, 2013 13:44
Generate an externs.js file for arbitrary JS libraries for use in advanced Google Closure compilation, based on the ClojureScript code that uses the libraries.
(ns n01se.externs-for-cljs
(:require [clojure.java.io :as io]
[cljs.compiler :as comp]
[cljs.analyzer :as ana]))
(defn read-file [file]
(let [eof (Object.)]
(with-open [stream (clojure.lang.LineNumberingPushbackReader. (io/reader file))]
(vec (take-while #(not= % eof)
(repeatedly #(read stream false eof)))))))