【发布时间】:2016-01-01 15:07:21
【问题描述】:
我正在尝试创建一个函数,该函数采用文件路径并将其内容作为字符串返回。这是我的代码 sn-p:
(defn get-string-from-file-path [path]
(let [fs (node/require "fs") c (chan)]
(.readFile fs path "utf8" (fn [err data] ((go (>! c (str data))))))
(go (def r (str (<! c)))) ; is using def correct here?
r))
- 在第 4 行使用
def是否正确/惯用? - 使用有效的文件路径调用此函数后,我收到以下错误:
TypeError:(中间值)(中间值)(中间值) value)(...).call 不是函数 在回复:99:6 在 tryToString (fs.js:414:3) 在 FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:401:12)
奇怪的是,如果我再次调用函数,我会得到文件所需的字符串内容!这是怎么回事?
编辑:这是更正后的函数:
(defn get-string-channel-from-file-path [path] ; function is now "get string channel" instead of "get string"
(let [fs (node/require "fs") c (chan)]
(.readFile fs path "utf8" (fn [err data] (go (>! c data))))
c)) ; return channel instead of string
【问题讨论】:
标签: node.js clojure clojurescript