Skip to content

Instantly share code, notes, and snippets.

@tangrammer
Created October 18, 2013 10:42
Show Gist options
  • Save tangrammer/7039755 to your computer and use it in GitHub Desktop.
Save tangrammer/7039755 to your computer and use it in GitHub Desktop.
a clojure core.async proposal solution to this question: "Clojure (script): macros to reason about async operations synchronously" http://stackoverflow.com/questions/11171066/clojure-script-macros-to-reason-about-async-operations-synchronously
(ns fourclojure.stack
(require [clojure.core.async :as async :refer :all]))
(defn update-sidebar! [new-data]
(println "you have updated the sidebar with this data:" new-data))
(defn async-handler [the-channel data-recieved]
(put! the-channel data-recieved)
)
(defn make-ajax-call [url data-to-send]
(let [the-channel (chan)]
(go
(<! (timeout 2000)); wait 2 seconds to response
(async-handler the-channel (str "return value with this url: " url)))
the-channel
)
)
(update-sidebar! (<!! (make-ajax-call "/fetch-new-data" {})))
@tangrammer
Copy link
Author

and the project configuration:

(defproject fourclojure "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
   :repositories {"sonatype-staging"
                 "https://oss.sonatype.org/content/groups/staging/"}
  :dependencies [
                 [org.clojure/clojure "1.5.1"]
                 [org.clojure/core.async "0.1.0-SNAPSHOT"]
                 ])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment