簡體   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