Skip to content

Instantly share code, notes, and snippets.

@grzm
Last active January 3, 2016 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grzm/8524580 to your computer and use it in GitHub Desktop.
Save grzm/8524580 to your computer and use it in GitHub Desktop.
Attempt to straight-line sequential callbacks using core.async
(ns node-async.core
(:require-macros [cljs.core.async.macros :refer [go]]
[node-async.macros :refer [<?]])
(:require [cljs.core.async :refer [take!]]
[node-async.task :refer [run-task]]))
;; run-task and <? appropriated from dnolen
;; https://gist.github.com/swannodette/6385166
;; https://github.com/swannodette/swannodette.github.com/blob/master/code/blog/src/blog/utils/macros.clj
;; https://github.com/swannodette/swannodette.github.com/blob/master/code/blog/src/blog/utils/helpers.cljs
(defn first-async [args callback]
(.log js/console (str "(first-async " args ")"))
(.nextTick js/process #(callback args)))
(defn second-async [args callback]
(.log js/console (str "(second-async " args ")"))
(.nextTick js/process #(callback args)))
(defn handler [args]
(.log js/console (str "(handler-2 " args ")"))
(go
(let [first-res (<? (run-task first-async args))
xformed (.toUpperCase first-res)
second-res (<? (run-task second-async xformed))]
(.log js/console (str "first-res: " first-res ))
(.log js/console (str "xformed: " xformed))
(.log js/console (str "second-res: " second-res)))))
(defn -main [& args]
(handler "something-to-handle")
(.log js/console "started main"))
(set! *main-cli-fn* -main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment