text: Q:
  
  Passing parameters to predefined callbacks in Javascript/NodeJs
  
  I'm trying to extend my simple signin method of a NodeJs backend with bcrypt. My problem is that I now need to pass the user variable through the hashing step. My current method looks like this -- and naturally user  is undefined in the scope of onPasswordHashed:
  signin : function(req, res, next) {
    step (
      function findUser() {
        User.findOne({ "email": req.body.email }, this);
      },
      function onResultReceived(error, user) {
        if (error) {
          ...
        } else {
          if (user) {
            bcrypt.compare(req.body.password, user.password, this);
          } else {
            ...
          }
        }     
      },
      function onPasswordHashed(error, hash) {
        if (error) {
          ...
        } else {
          bcrypt.compare(user.password, hash, this); // user is undefined here
        }
      },
      ...
    );
  },
  
  In principle I can: (a) Use the synchronous call of bcrypt. However, at some point I might encounter the same issue where no synchronous function call is available. (b) I can define var userObj = null at first and set it userObj = user in the onResultReceived method. userObj should then be visible in all scopes. But that doesn't seem to be a best practice. Or does it?
  From reading up so far, using bind() seems the way to go. I just don't get my head around it how to apply it to my. For example:
  bcrypt.compare(req.body.password, user.password, this.bind({user: user}));
  
  doesn't work. I don't know if the step package might cause any issues here. It's just very convenient to handle chains of callbacks.
  EDIT: Links to step package documentation: npm, github
  From what I found and tested, I can overwrite the callback, e.g.:
  bcrypt.compare(req.body.password, user.password, this(user));
  
  But with this I of course loose information about error and hash.
  
  A:
  
  You can either use an inline anonymous callback that can directly access variables in the parent scope of you can use .bind() to add arguments to the callback or in your particular sequence case, you can save the user object to a higher scoped variable so it is available to subsequent callbacks.   See the localUser variable in this example:
  signin : function(req, res, next) {
    var localUser;
    step (
      function findUser() {
        User.findOne({ "email": req.body.email }, this);
      },
      function onResultReceived(error, user) {
        // save user variable to higher scoped variable so
        // subsequent callbacks can access it
        localUser = user;
        if (error) {
          ...
        } else {
          if (user) {
            bcrypt.compare(req.body.password, user.password, this);
          } else {
            ...
          }
        }     
      },
      function onPasswordHashed(error, hash) {
        if (error) {
          ...
        } else {
          // use localUser from higher scope here that was set by a previous
          // step in the process
          bcrypt.compare(localUser.password, hash, this);
        }
      },
      ...
    );
  },
  
  FYI, if you provide some info or link to doc on how the step() function works, there may also be a way to pass data from one step onto the next steps.
  
  Assuming the step() function comes from this module, you could also do it like this:
  signin : function(req, res, next) {
    step (
      function findUser() {
        User.findOne({ "email": req.body.email }, this);
      },
      function onResultReceived(error, user) {
        // save user on our stepper object so it can be accessed by later callbacks
        this.user = user;
        if (error) {
          ...
        } else {
          if (user) {
            bcrypt.compare(req.body.password, user.password, this);
          } else {
            ...
          }
        }     
      },
      function onPasswordHashed(error, hash) {
        if (error) {
          ...
        } else {
          // use this.user that was set by a previous
          // step in the process
          bcrypt.compare(this.user, hash, this);
        }
      },
      ...
    );
  },
  
  The value of this as passed to each successive callback is a common function object that you can attach your own properties to.  While this sort of seems cleaner than the previous version, it's actually a little more dangerous because properties you add to the this object could conflict with things used in the internal implementation of the step() function.  The first option (an object in the parent scope) is totally private and won't have that possible conflict.
  
  Now understanding more about how step() works, you can use .bind() to add the user object to the next callback arguments like this:
  signin : function(req, res, next) {
    step (
      function findUser() {
        User.findOne({ "email": req.body.email }, this);
      },
      function onResultReceived(error, user) {
        // save user on our stepper object so it can be accessed by later callbacks
        if (error) {
          ...
        } else {
          if (user) {
            // prepend user to the callback arguments for the next callback
            bcrypt.compare(req.body.password, user.password, this.bind(this, user));
          } else {
            ...
          }
        }     
      },
      function onPasswordHashed(user, error, hash) {
        // user is passed in here from previous callback
        if (error) {
          ...
        } else {
          // use this.user that was set by a previous
          // step in the process
          bcrypt.compare(user, hash, this);
        }
      },
      ...
    );
  },
  
  