How to pass variable to a nested callback function

I'm using request npm to get http pages. I have something like that

function callbackFunction1() {
  var p = 'foo';

  request(url, callbackFunction2);
}

function callbackFunction2(err, response, body){

}

How could I pass the variable p as parameter of the callbackFunction2?

I think you need to give all 3 parameters.

You can pass an anonymous function as second parameter like bellow

request(url, function(err, response, body){
    callbackFunction2(err,response, body, p);
});

@facebook 's comment is they way to go.

Use bind

request(url, callbackFunction2.bind(p));
...
console.log(this); // p

Read @facebook 's link to see how to do it without messing this if it matters in your use case.

Try to define p as a global variable. This should work 99%

var p;

function callbackFunction1() {
  p = 'foo';

  request(url, callbackFunction2);
}

function callbackFunction2(err, response, body){
    console.log(p)
}