Search code examples
clojurescriptcore.async

Can't use async functions in clojurescript


I am trying to use a npm package in cljs called "systeminformation"
most of its function are async and some are non-async
but I am unable to use async function, everything else work fine
RELATED IMPORTS

[clojure.core.async :as async] 
["systeminformation" :as systeminformation]

Code I am trying to run

(comment
  (systeminformation/version) // WORKS FINE 
  (async/go
    (async/<! (systeminformation/cpu))) // Gives me error 
  )

ERROR:

INFO [mutesync.inspect.electron.background.main:30] - STACK
 TypeError: c.cljs$core$async$impl$protocols$ReadPort$take_BANG_$arity$2 is not a function
    at cljs$core$async$impl$ioc_helpers$take_BANG_ (D:\Tom\mutesync\.shadow-cljs\builds\electron-main\dev\out\cljs-runtime\cljs\core\async\impl\ioc_helpers.cljs:52:1)
    at switch__47338__auto__ (<eval>:8:52)
    at <eval>:32:29
    at Function.fexpr__47378 [as cljs$core$IFn$_invoke$arity$1] (<eval>:54:4)
    at Object.cljs$core$async$impl$ioc_helpers$run_state_machine [as run_state_machine] (D:\Tom\mutesync\.shadow-cljs\builds\electron-main\dev\out\cljs-runtime\cljs\core\async\impl\ioc
_helpers.cljs:43:3)
    at cljs$core$async$impl$ioc_helpers$run_state_machine_wrapped (D:\Tom\mutesync\.shadow-cljs\builds\electron-main\dev\out\cljs-runtime\cljs\core\async\impl\ioc_helpers.cljs:45:1)
    at <eval>:84:67
    at Immediate.cljs$core$async$impl$dispatch$process_messages (D:\Tom\mutesync\.shadow-cljs\builds\electron-main\dev\out\cljs-runtime\cljs\core\async\impl\dispatch.cljs:26:7)
    at processImmediate (internal/timers.js:456:21)
ERROR [mutesync.inspect.electron.background.main:68] - uncaught error
TypeError: c.cljs$core$async$impl$protocols$ReadPort$take_BANG_$arity$2 is not a function

Solution

  • Async functions in JS are syntax sugar for functions returning a Promise.

    core.async does not work with Promises by default and you need to use the helper function to make them act like channels if you want to. The <p! macro does this for you.

    (ns test.foo
      (:require
        [clojure.core.async :as async] 
        [cljs.core.async.interop :refer (<p!)]
        ["systeminformation" :as systeminformation]))
    
    
    (async/go
      (prn (<p! (systeminformation/cpu))))
    

    Alternatively you can just .then or .catch the Promise with a callback. There is no need for core.async when doing so.

    (-> (systeminformation/cpu)
        (.then prn))
    

    There is also a guide available on the topic.