简体   繁体   English

如何将回调作为变量传递给预定义的回调

[英]How to pass callback as variable to predefined callback

I have some code written in LiveScript (fork of Coffeescript) that will make a request using the request module, and then pass the statusCode of the response to a callback. 我有一些用LiveScript(Coffeescript的叉子)编写的代码,这些代码将使用请求模块发出请求,然后将响应的statusCode传递给回调。

url = 'http://www.google.ca'

logCode = (statusCode) ->
  console.log statusCode

makeRequest = (url, callback) ->
  request url, (err, response, body) ->
    if body
      callback response.statusCode
    else
      console.log "No response"

makeRequest url, logCode

This works as expected, and the callback is triggered successfully and '200' is logged to the console. 这将按预期方式工作,并且回调已成功触发,并且将'200'记录到控制台。

However, I would like to make my code as clean as possible, and pull out any nested callbacks. 但是,我想使我的代码尽可能干净,并取出所有嵌套的回调。 Therefore I define a function to act as my request callback and pass it in as the second argument to request. 因此,我定义了一个函数来充当我的请求回调,并将其作为第二个参数传递给request。

url = 'http://www.google.ca'

requestCallback = (err, response, body) ->
  if body
    callback response.statusCode
  else
    console.log "No response"

logCode = (statusCode) ->
  console.log statusCode

makeRequest = (url, callback) ->
  request url, requestCallback

makeRequest url, logCode

However, running this of course returns an error saying that callback is not defined. 但是,当然运行此命令会返回一条错误消息,说明未定义回调。 This of course makes perfect sense, as in the scope that requestCallback operates, there is no function named callback, and it cannot access the callback defined in makeRequest. 这当然是很合理的,因为在requestCallback操作的范围内,没有名为callback的函数,并且它无法访问makeRequest中定义的回调。

From what I've read, I can declare requestCallback inside an anonymous function, which will allow it to have callback passed into it. 根据我的阅读,我可以在一个匿名函数中声明requestCallback,这将使它能够将回调传递给它。 I've tried this is every manner I could conceive of without fruition. 我尝试过这是我无法实现的所有构想。

Also, there seems to be the option of binding requestCallback to the scope of makeRequest, however I was unable to do this today as well despite my best efforts and wrangling. 另外,似乎可以将requestCallback绑定到makeRequest的范围,但是尽管我尽了最大的努力和努力,我今天也无法做到这一点。

How can I allow requestCallback to be aware of the callback function passed to makeRequest? 如何允许requestCallback知道传递给makeRequest的回调函数?

Not sure how you would write it in LiveScript but in JavaScript its: 不知道如何在LiveScript中编写它,但是在JavaScript中:

function makeRequest(url, callback) {
    request(url, function (err, response, body) {
        requestCallback(err, response, body, callback);
    });
}

So basically the callback you pass into request calls requestCallback with another argument. 因此,基本上,您传递给request的回调将使用另一个参数来调用requestCallback

If you're doing a lot of asynchronous stuff it's worthwhile to look into Promises . 如果您正在做很多异步工作,那么值得研究Promises They can really help with avoiding callback-spaghetti. 他们真的可以帮助避免意大利面条。

With Promises you could do something like: 使用Promises,您可以执行以下操作:

makeRequest(url).done(function (response, body) {
    if (body) {
        logCode(response.statusCode)
    } else {
        console.log("No response");
    }
}).fail(function (response) {
    throw new Error("uhoh!");
});

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

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