Coffeescript异步和范围

时间:2015-06-22 02:06:18

标签: javascript http asynchronous coffeescript hubot

我有一个我正在提取和处理的URL数组,我希望在数据返回时能够引用它们的键。在数据返回时,确保将异步HTTP调用之前确定的值传递到闭包中的首选方法是什么?在下面的示例中,当来自HTTP调用的数据返回时,calendar_name变量(如预期的那样)丢失,并且我需要能够在闭包内引用该值:

cals = {
  "calendar one":"http://example.com/info/webcal/abc123.ics",
  "calendar two":"http://example.com/info/webcal/xyz789.ics"
}
for calendar_name,calendar_url of cals
  # Note that this is a Hubot script and the following line performs an HTTP call
  msg.http(calendar_url)
    .get() (err, res, body) ->
      if res.statusCode is 200 and !err?
        ics = ical.parseICS(body)
        event_list = []
        for _, event of ics
          if event.type == 'VEVENT'
            event_list.push event.summary
        console.log("Scheduled events for " + calendar_name)
        for event, k in event_list
          console.log(event)

0 个答案:

没有答案