CoffeeScript Hubot帮助:从函数

时间:2016-03-21 23:29:25

标签: 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, (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代码?

谢谢!

1 个答案:

答案 0 :(得分:0)

伪代码:

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