Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve and reject pormises with cljs-ajax #4

Closed
JarrodCTaylor opened this issue Jul 17, 2015 · 2 comments
Closed

Resolve and reject pormises with cljs-ajax #4

JarrodCTaylor opened this issue Jul 17, 2015 · 2 comments

Comments

@JarrodCTaylor
Copy link
Contributor

I am attempting to use the promesa library with cljs-ajax. I am unsure how to resolve / reject a promise in conjunction with the requests generated from that library.

Is this something that you have any experience with?

I am attempting to do something similar to the following.

(def user-info (ratom/atom {:username "" :password "" :email ""}))

;; ====
;  Registration
(defn register-error-handler [{:keys [status status-text]}]
  (.log js/console "Registration error"))

(defn register-response-handler [response]
  (.log js/console "Registration was a success"))

(defn register-new-user []
  (POST "https://example.com/api/user" {:params        {:password (:password @user-info)
                                                        :username (:username @user-info)
                                                        :email    (:email    @user-info)}
                                        :handler       register-response-handler
                                        :error-handler register-error-handler}))

;; ====
;  Login
(defn login-error-handler [{:keys [status status-text]}]
  (.log js/console "Login Error"))

(defn login-response-handler [response]
  (.log js/console "Handle login"))

(defn auth-header [username password]
  (str "Basic " (b64/encodeString (str username ":" password))))

(defn attempt-login [username password]
  (GET "https://example.com/api/auth" {:headers         {"Authorization" (auth-header username password)}
                                       :handler         login-response-handler
                                       :error-handler   login-error-handler
                                       :response-format :json
                                       :keywords?       true
                                       :prefix          true}))

;; ==========
;  What is the proper way to resove/reject the promise when registering a user?

(defn register-and-login-new-user []
  (-> (p/promise register-new-user)
      (p/then    (sf/attempt-login (:username @user-info) (:password @user-info)))))
@niwinz
Copy link
Member

niwinz commented Jul 18, 2015

It there different way to do that.

Your register function should be something similar to:

(defn register-user
  [params]
  (p/promise
   (fn [resolve reject]
     (http/post url {:params params
                     :handler resolve
                     :error-handler reject}))))

Then, you can use it in different ways, depending on when you want handle the error case.

This one uses the default promise chaining functions:

(-> (register-user {:username "foo"
                    :password "bar"})
    (p/then #(.log js/console "Success:" %))
    (p/catch #(.error js/console "Error:" %)))

Also, you can use let instead of thread macro:

(let [result (register-user {:username "foo"
                             :password "bar"})]
  (p/then result #(.log js/console "Success:" %))
  (p/catch result #(.error js/console "Error:" %)))

Or inclusive you can use the monadic binding using cats:

(require '[cats.core :as m])

(m/mlet [result (register-user {:username "foo"
                                :password "bar"})]
  ;; The mlet body will be executed only if the
  ;; result will successfully resolved.
  (.log js/console "Success " result))

@niwinz niwinz closed this as completed Jul 18, 2015
@JarrodCTaylor
Copy link
Contributor Author

Thanks! Very clear examples, makes complete sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants