CoffeeScript Hubot 帮助:从函数获取数据

CoffeeScript Hubot help: getting data from a function

我是新手,需要一些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 代码?

谢谢!

伪代码:

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