{"action":"created","issue":{"url":"https://api.github.com/repos/JulianBirch/cljs-ajax/issues/155","repository_url":"https://api.github.com/repos/JulianBirch/cljs-ajax","labels_url":"https://api.github.com/repos/JulianBirch/cljs-ajax/issues/155/labels{/name}","comments_url":"https://api.github.com/repos/JulianBirch/cljs-ajax/issues/155/comments","events_url":"https://api.github.com/repos/JulianBirch/cljs-ajax/issues/155/events","html_url":"https://github.com/JulianBirch/cljs-ajax/issues/155","id":169093670,"number":155,"title":"Implement an AjaxImpl that uses js/Fetch","user":{"login":"JulianBirch","id":84115,"avatar_url":"https://avatars.githubusercontent.com/u/84115?v=3","gravatar_id":"","url":"https://api.github.com/users/JulianBirch","html_url":"https://github.com/JulianBirch","followers_url":"https://api.github.com/users/JulianBirch/followers","following_url":"https://api.github.com/users/JulianBirch/following{/other_user}","gists_url":"https://api.github.com/users/JulianBirch/gists{/gist_id}","starred_url":"https://api.github.com/users/JulianBirch/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JulianBirch/subscriptions","organizations_url":"https://api.github.com/users/JulianBirch/orgs","repos_url":"https://api.github.com/users/JulianBirch/repos","events_url":"https://api.github.com/users/JulianBirch/events{/privacy}","received_events_url":"https://api.github.com/users/JulianBirch/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/JulianBirch/cljs-ajax/labels/PR%20welcome","name":"PR welcome","color":"009800"}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-08-03T10:00:45Z","updated_at":"2016-08-03T14:43:27Z","closed_at":null,"body":"This hasn't been fully investigated but a) it would future proof cljs-ajax and b) it would solve the ReactNative problem. Cross-reference #141 "},"comment":{"url":"https://api.github.com/repos/JulianBirch/cljs-ajax/issues/comments/237257235","html_url":"https://github.com/JulianBirch/cljs-ajax/issues/155#issuecomment-237257235","issue_url":"https://api.github.com/repos/JulianBirch/cljs-ajax/issues/155","id":237257235,"user":{"login":"pesterhazy","id":106328,"avatar_url":"https://avatars.githubusercontent.com/u/106328?v=3","gravatar_id":"","url":"https://api.github.com/users/pesterhazy","html_url":"https://github.com/pesterhazy","followers_url":"https://api.github.com/users/pesterhazy/followers","following_url":"https://api.github.com/users/pesterhazy/following{/other_user}","gists_url":"https://api.github.com/users/pesterhazy/gists{/gist_id}","starred_url":"https://api.github.com/users/pesterhazy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pesterhazy/subscriptions","organizations_url":"https://api.github.com/users/pesterhazy/orgs","repos_url":"https://api.github.com/users/pesterhazy/repos","events_url":"https://api.github.com/users/pesterhazy/events{/privacy}","received_events_url":"https://api.github.com/users/pesterhazy/received_events","type":"User","site_admin":false},"created_at":"2016-08-03T14:43:27Z","updated_at":"2016-08-03T14:43:27Z","body":"I can confirm that using fetch works fine on RN, so this would be a good approach\r\n\r\nHere code that may serve as a good starting point for a fetch backend for cljs-ajax:\r\n\r\n```\r\n(-> (js/fetch uri (clj->js m))\r\n        (.then (fn [r]\r\n                 (if (.-ok r)\r\n                   ;; good\r\n                   (let [content-type (-> r .-headers (.get \"content-type\"))]\r\n                     (if (clojure.string/includes? content-type \"application/transit+json\")\r\n                       (.text r)\r\n                       (throw (ex-info \"Content-type is not transit\" {:content-type content-type}))))\r\n                   ;; bad\r\n                   (.then (.text r)\r\n                          (fn [text]\r\n                            (let [payload (transit/read transit-reader text)\r\n                                  err {:status (.-status r)\r\n                                       :status-text (.-statusText r)\r\n                                       :failure :error\r\n                                       :response payload}]\r\n                              (throw err)))))))\r\n        (.then (fn [text]\r\n                 (transit/read transit-reader text)))\r\n        (.then handler #(error-handler (if (map? %) % {:message \"Fetch promise failed\" :reason %})))\r\n        ;; in case of a handler error, break out of the try block\r\n        ;; to throw an actual exception\r\n        (.catch (fn [err] (js/setTimeout #(throw err)))))\r\n```\r\n\r\nAs you can see, I struggle with throwing regular exception out of a promise-based api. The problem, essentially, is that `.then` and `.catch` methods are all wrapped in `try` blocks. So if we just call the handler in a `.then` method, exceptions in that functions will be swallowed, or reported as network request errors, neither of which is particularly desirable.\r\n\r\nAs a workaround I use `setTimeout` in the code, but this may not be an optimal solution.\r\n\r\nAlso the transit-related code is probably not applicable as cljs-ajax does that by itseslf."}}
{"actor":{"display_login":"pesterhazy"}}