CoffeeScript将子进程结果传递给变量
我正在试图理解为什么我的代码没有将CoffeeScript将子进程结果传递给变量,coffeescript,Coffeescript,我正在试图理解为什么我的代码没有将stdout传递到taskList,在childp.exec内部msg.send工作正常,但taskList始终是空字符串 cmd = "cd #{directory}; \ bundle exec rake -T | perl -lne 'print if /^rake (clear|integration):/'" taskList = "" childp = require 'child_process' childp.exec cmd, (
stdout
传递到taskList
,在childp.exec
内部msg.send
工作正常,但taskList
始终是空字符串
cmd = "cd #{directory}; \
bundle exec rake -T | perl -lne 'print if /^rake (clear|integration):/'"
taskList = ""
childp = require 'child_process'
childp.exec cmd, (error, stdout, stderr) =>
if error?
msg.send ":x: Error to get rake task lists"
return
else
msg.send stdout # Works fine, I can see a list of rake tasks I want
taskList = stdout
msg.send typeof taskList # Return 'string'
msg.send taskList # empty string is sent back
有什么建议吗?exec不是异步的吗?如果是这样,您必须等待回调运行,然后才能访问
taskList
@Thilo我该如何等待?我的意思是,在编码中,是否有同步版本的exec?您需要将代码移动到该回调中(在taskList=stdout
之后)。或者用承诺之类的东西。可能是