[英]Why can't I pass Clojurescript functions as callbacks to Javascript?
[英]Can't use async functions in clojurescript
我正在嘗試在名為“systeminformation”的 cljs 中使用 npm package
它的大部分 function 是異步的,有些是非異步的
但我無法使用異步 function,其他一切正常
相關進口
[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
JS 中的異步函數是返回Promise
的函數的語法糖。
core.async
默認情況下不適用於 Promises,如果需要,您需要使用幫助程序 function 使它們像通道一樣。 <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
Promise。 這樣做時不需要core.async
。
(-> (systeminformation/cpu)
(.then prn))
還有一個關於該主題的指南。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.