Last active
January 24, 2018 11:23
-
-
Save benzap/1b8f50ed37a6b5c5e92c05d4b0115656 to your computer and use it in GitHub Desktop.
Working JSONP example using goog.net Jsonp returning async channels
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (ns redditv.jsonp | |
| (:require-macros [cljs.core.async.macros :refer [go]]) | |
| (:require [cljs.core.async :refer [put! chan <! close!]]) | |
| (:import [goog.net Jsonp] | |
| [goog Uri])) | |
| (defn send-jsonp [url] | |
| (let [success-channel (chan) | |
| error-channel (chan) | |
| success-handler (fn [result] (if-not (nil? result) | |
| (put! success-channel result) | |
| (do | |
| (close! success-channel) | |
| (close! error-channel)))) | |
| error-handler (fn [result] (if-not (nil? result) | |
| (put! error-channel result) | |
| (do | |
| (close! error-channel) | |
| (close! success-channel)))) | |
| jsonp (goog.net.Jsonp. (Uri. url) "jsonp") | |
| ] | |
| (.send jsonp nil success-handler error-handler) | |
| [success-channel error-channel] | |
| )) | |
| #_(def test-url "http://www.reddit.com/r/videos.json") | |
| #_(let [[success failure] (send-jsonp test-url)] | |
| (go (let [result (<! success)] | |
| (.log js/console "Success:" result))) | |
| (go (let [result (<! failure)] | |
| (println "Fail: " result)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment