从 hubot 脚本中的嵌套匿名函数返回结果
Returning result from nested anonymous function in a hubot script
之前从未使用过 coffescript,我正在尝试更新 hubot script for jenkins integration。简而言之,我想调用 jenkins,从该调用中获取结果并在后续调用中使用它。基于 hubot 脚本中的现有代码,我添加了以下函数:
jenkinsCrumb = (msg) ->
url = process.env.HUBOT_JENKINS_URL
path = "#{url}/crumbIssuer/api/json"
req = msg.http(path)
if process.env.HUBOT_JENKINS_AUTH
auth = new Buffer(process.env.HUBOT_JENKINS_AUTH).toString('base64')
req.headers Authorization: "Basic #{auth}"
req.get() (err, res, body) ->
if err
msg.reply "Jenkins says: #{err}"
else if 200 <= res.statusCode < 400 # Or, not an error code.
msg.reply "#{body}"
body
else if 404 == res.statusCode
msg.reply "Unable to fetch crumb from Jenkins..."
else
msg.reply "Jenkins says: Status #{res.statusCode} #{body}"
当这个函数被调用时,我想要的值被报告在变量body
中。对 msg.reply
的调用在 hubot 聊天 window.
中正确显示了值
我想做的,但想不通的是,如何让这个函数return成为body
的值?我已经尝试明确 returning req.get()
的值,但它似乎是 returning 完整的请求对象。
您可以通过简单地在匿名函数的末尾添加 return body
或 body
(因为 CoffeeScript)来做到这一点:
jenkinsCrumb = (msg, callback) ->
url = process.env.HUBOT_JENKINS_URL
path = "#{url}/crumbIssuer/api/json"
req = msg.http(path)
if process.env.HUBOT_JENKINS_AUTH
auth = new Buffer(process.env.HUBOT_JENKINS_AUTH).toString('base64')
req.headers Authorization: "Basic #{auth}"
req.get() (err, res, body) ->
if err
msg.reply "Jenkins says: #{err}"
else if 200 <= res.statusCode < 400 # Or, not an error code.
msg.reply "#{body}"
body
else if 404 == res.statusCode
msg.reply "Unable to fetch crumb from Jenkins..."
else
msg.reply "Jenkins says: Status #{res.statusCode} #{body}"
# critical part
callback(body)
之前从未使用过 coffescript,我正在尝试更新 hubot script for jenkins integration。简而言之,我想调用 jenkins,从该调用中获取结果并在后续调用中使用它。基于 hubot 脚本中的现有代码,我添加了以下函数:
jenkinsCrumb = (msg) ->
url = process.env.HUBOT_JENKINS_URL
path = "#{url}/crumbIssuer/api/json"
req = msg.http(path)
if process.env.HUBOT_JENKINS_AUTH
auth = new Buffer(process.env.HUBOT_JENKINS_AUTH).toString('base64')
req.headers Authorization: "Basic #{auth}"
req.get() (err, res, body) ->
if err
msg.reply "Jenkins says: #{err}"
else if 200 <= res.statusCode < 400 # Or, not an error code.
msg.reply "#{body}"
body
else if 404 == res.statusCode
msg.reply "Unable to fetch crumb from Jenkins..."
else
msg.reply "Jenkins says: Status #{res.statusCode} #{body}"
当这个函数被调用时,我想要的值被报告在变量body
中。对 msg.reply
的调用在 hubot 聊天 window.
我想做的,但想不通的是,如何让这个函数return成为body
的值?我已经尝试明确 returning req.get()
的值,但它似乎是 returning 完整的请求对象。
您可以通过简单地在匿名函数的末尾添加 return body
或 body
(因为 CoffeeScript)来做到这一点:
jenkinsCrumb = (msg, callback) ->
url = process.env.HUBOT_JENKINS_URL
path = "#{url}/crumbIssuer/api/json"
req = msg.http(path)
if process.env.HUBOT_JENKINS_AUTH
auth = new Buffer(process.env.HUBOT_JENKINS_AUTH).toString('base64')
req.headers Authorization: "Basic #{auth}"
req.get() (err, res, body) ->
if err
msg.reply "Jenkins says: #{err}"
else if 200 <= res.statusCode < 400 # Or, not an error code.
msg.reply "#{body}"
body
else if 404 == res.statusCode
msg.reply "Unable to fetch crumb from Jenkins..."
else
msg.reply "Jenkins says: Status #{res.statusCode} #{body}"
# critical part
callback(body)