我试图在cljs中使用一个名为“系统信息”的npm包。
它的大部分功能是异步的,有些是非异步的。
但是我不能使用异步函数,其他功能都很好。
相关进口
[clojure.core.async :as async]
["systeminformation" :as systeminformation]代码我正在尝试运行
(comment
(systeminformation/version) // WORKS FINE
(async/go
(async/<! (systeminformation/cpu))) // Gives me 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发布于 2021-04-04 17:17:24
JS中的异步函数是返回Promise的函数的语法糖。
默认情况下,core.async不适用于承诺,如果您愿意,您需要使用助手函数来使它们像通道一样工作。<p!宏为您执行此操作。
(ns test.foo
(:require
[clojure.core.async :as async]
[cljs.core.async.interop :refer (<p!)]
["systeminformation" :as systeminformation]))
(async/go
(prn (<p! (systeminformation/cpu))))或者,您可以只使用.then或.catch进行回调。在这样做时,不需要core.async。
(-> (systeminformation/cpu)
(.then prn))还有一个关于这个主题的指南。
https://stackoverflow.com/questions/66941461
复制相似问题