繁体   English   中英

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM