无法在clojurescript中使用异步函数
我想在cljs称为使用NPM包“systeminformation”
它的大部分功能都是异步的,有些是不异步
,但我无法使用异步功能,一切工作正常
有关的进口
[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:Tommutesync.shadow-cljsbuildselectron-maindevoutcljs-runtimecljscoreasyncimplioc_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:Tommutesync.shadow-cljsbuildselectron-maindevoutcljs-runtimecljscoreasyncimplioc
_helpers.cljs:43:3)
at cljs$core$async$impl$ioc_helpers$run_state_machine_wrapped (D:Tommutesync.shadow-cljsbuildselectron-maindevoutcljs-runtimecljscoreasyncimplioc_helpers.cljs:45:1)
at <eval>:84:67
at Immediate.cljs$core$async$impl$dispatch$process_messages (D:Tommutesync.shadow-cljsbuildselectron-maindevoutcljs-runtimecljscoreasyncimpldispatch.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
回答
JS 中的异步函数是返回Promise.
core.async默认情况下不与 Promise 一起使用,如果您愿意,您需要使用辅助函数使它们像通道一样工作。该<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))))
或者,您可以只使用 Promise.then或.catch带有回调的 Promise。core.async这样做时不需要。
(-> (systeminformation/cpu)
(.then prn))
还有一个关于该主题的指南。