简体   繁体   English

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

[英]CoffeeScript Hubot help: getting data from a function

I'm a novice in need of some Hubot/CoffeeScript help. 我是新手,需要一些Hubot / CoffeeScript帮助。

I have several responses which will get data from the same source but use and respond to different pieces of the payload. 我有几个响应,这些响应将从同一个源获取数据,但是使用并响应不同的负载。 For example... 例如...

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

The process of building the URL for the score data includes looking up the current month, day and year. 为得分数据构建URL的过程包括查找当前的月,日和年。 I don't want to do that more than once but I will have many responses like the above which use the same data. 我不想重复做一次,但是我会收到很多类似的使用相同数据的响应。 I had expected I could do this. 我以为我可以做到这一点。

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'

I guess this doesn't work because it's async. 我猜这是行不通的,因为它是异步的。 But, doing msg.send from within the getScore function doesn't work. 但是,从getScore函数中执行msg.send不起作用。 So, how do I do this so that I don't have to repeat the getScroe code in every robot.hear section? 因此,我该如何做而不必在每个robot.hear部分中重复getScroe代码?

Thanks! 谢谢!

Pseudo code: 伪代码:

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