CoffeeScript Hubot帮助:从函数获取数据
我是个新手,需要Hubot/CoffeeScript的帮助 我有几个响应,它们将从同一个源获取数据,但使用和响应不同的有效负载片段。例如CoffeeScript Hubot帮助:从函数获取数据,coffeescript,hubot,Coffeescript,Hubot,我是个新手,需要Hubot/CoffeeScript的帮助 我有几个响应,它们将从同一个源获取数据,但使用和响应不同的有效负载片段。例如 module.exports = (robot) -> robot.hear /score/i, (msg) -> score = getScore(today) msg.send "today's score is " + score robot.hear /yesterday's score/i,
module.exports = (robot) ->
robot.hear /score/i, (msg) ->
score = getScore(today)
msg.send "today's score is " + score
robot.hear /yesterday's score/i, (msg) ->
score = getStore(yesterday) ->
msg.send "yesterday's score was " + score
为分数数据构建URL的过程包括查找当前月份、日期和年份。我不想这样做超过一次,但我会有很多像上面一样使用相同数据的回复。我原以为我能做到这一点
getScore = (day) ->
#build the url and get the data set
#pick the right piece of data based on the day variable and assign it to score'
我想这不起作用,因为它是异步的。但是,在getScore函数中执行msg.send不起作用。那么,我如何做到这一点,以使我不必在每个robot.hear部分重复getScroe代码
谢谢 伪代码:
getScore = (day, callback) ->
# get the score...
callback(score)
robot.hear /yesterday's score/i, (msg) ->
getScore "yesterday", (score) ->
msg.send "yesterday's score was " + score
知道了!谢谢eostergerg!我仍在努力研究异步函数。