This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-05-10
Channels
- # babashka (35)
- # beginners (70)
- # bristol-clojurians (1)
- # calva (30)
- # cider (2)
- # cljsrn (203)
- # clojure (20)
- # clojure-australia (3)
- # clojure-europe (17)
- # clojure-france (7)
- # clojure-nl (4)
- # clojure-uk (8)
- # clojurescript (27)
- # code-reviews (19)
- # core-async (21)
- # cursive (7)
- # datomic (31)
- # defnpodcast (1)
- # duct (6)
- # emacs (3)
- # events (1)
- # helix (2)
- # honeysql (24)
- # jackdaw (2)
- # jobs (2)
- # jobs-discuss (13)
- # juxt (2)
- # lsp (21)
- # re-frame (45)
- # remote-jobs (2)
- # rewrite-clj (3)
- # shadow-cljs (13)
- # vim (1)
Hello,
what is the idiomatic way of dealing with promises?
I have found the official promise interop guide and <p!
but I am not quite sure how to apply that to my scenario.
I need to get the access-token from AWS amplify where currentSession
returns a promise.
I then want to unpack the value of the token pass it along and then make a network call if the promise resolves.
Current code:
(defn access-token []
(-> (.currentSession Auth)
(.then #(.. % (getAccessToken) (getJwtToken)))))
(defn call [access-token]
(ajax/GET url
{:format :json
:response-format :json
:keywords? true
:headers {"Authorization" access-token}
:handler handle-success
:error-handler handle-error}))
(defn load-wallet []
(update-loading true)
(-> (access-token)
(.then #(call %))
(.catch #(handle-error %))))
Is there a better way to write this code.There are multiple options to write this code. You can either use clojure.core.async or some 3rd party library like https://github.com/funcool/promesa or https://github.com/athos/kitchen-async etc. My recommendation is stick to plain promises or maybe use bluebird. I don't use anymore clojure.core.async on Clojurescript side since it 1. produces quite large code upon macroexpansion 2. if you use asynchronous context (node.js AsyncStorage) and fibers then information will leak If all modern browsers would support async/await and Clojurescript would support this syntax then I would probably stick to async/await 😄
promesa is just a syntactic abstraction over plain promises, so it does not introduces external dependencies more than the promesa itself
additionally to basic operations it alwo offers let
and do
like macros for easy compose promises
in difference to core.async interop, the result of composition of promises is always an other promise
Yep that is basically the best library for dealing with promises on clojurescript side. Thank you so much for this project!
Promesa looks very cool. Might come in handy if things get more complex
@U06B73YQJ is it possible to switch to bluebird implementation on promesa?
Something like this?
(ns infrastructure.patch
(:require
["bluebird" :as b]
[promesa.core :as p]
[promesa.impl :as impl]))
(impl/extend-promise! js/Promise)
(impl/extend-promise! b/Promise)
(p/set-default-promise! b/Promise)
Ok got it. Btw I'm very happy that you removed async-cljs namespace
Yeah, the same here, I just want a simple syntactic abstraction without additional deps
As I understand it's possible to just use p/let to simulate async/await alike features.
(-> (p/let [a (some-promise)
b (some-promise)]
(+ a b))
(p/catch (fn [err] (println "Some arithmetic error")))
What do you think about adding a some sugar like p/try+ p/catch+ p/finally+ so that the asynchronous nature is hidden when using p/let?
Actually that would make the whole point of "implement async/await on Clojurescript" invalid since all the futures that async/await provides would be given by promesa.I'm open to have try/catch/finally if someone want to contribute it. We use promesa extensivelly and practically never needed that macros
additionally, this macros are complex, and introduces a lot of hidden overhead to the code
@jakob.durstberger writing directly like that is fine
Thanks all 🙂