0

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.

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.

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.

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.

From what I've read, I can declare requestCallback inside an anonymous function, which will allow it to have callback passed into it. 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.

How can I allow requestCallback to be aware of the callback function passed to makeRequest?

1 Answer 1

2

Not sure how you would write it in LiveScript but in JavaScript its:

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.

If you're doing a lot of asynchronous stuff it's worthwhile to look into Promises. They can really help with avoiding callback-spaghetti.

With Promises you could do something like:

makeRequest(url).done(function (response, body) {
    if (body) {
        logCode(response.statusCode)
    } else {
        console.log("No response");
    }
}).fail(function (response) {
    throw new Error("uhoh!");
});
Sign up to request clarification or add additional context in comments.

1 Comment

Cool, thank you, I think I will use this for the meantime. Today I wanted to see how far so called clean callbacks could take me, but definitely in the future I will be looking into promises and generators. Thanks again for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.