Fork me on GitHub
#clojurescript
<
2021-04-04
>
Atiq Gauri12:04:05

I am trying to use a npm package in cljs called "https://github.com/sebhildebrandt/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

alpox12:04:21

Async functions from javascript are functions which return a javascript Promise. async.core would require a clojurescript core.async channel to work. With promises you can work as normally with js promises or use a helper library like https://github.com/funcool/promesa

Atiq Gauri13:04:38

yes but I wanted to avoid any library use this works too:

(.then (js/Promise.resolve (systeminformation/cpu))
         #(log/spy (js->clj %)))

p-himik13:04:55

Why do you use .resolve? Unwrap the call to (.../cpu)`.

p-himik13:04:03

(-> (systeminformation/cpu) (.then (fn [result] ...)))

🙌 3
✅ 3