简体   繁体   中英

How to add extra parameters to a function callback

I am using a few callbacks in an app that I'm writing. I am using Mongoose models and need to save a few different places. The save function takes a callback, and the callback gets error and model for its parameters, but I'd like to send the callback an extra parameter that the function needs. I'm not sure of the proper syntax to be able to do this. Below is some example code of what I'm going for...

var saveCallBack = function(err, model, email_address){
    if(err) {
        //handle error
    }

    else {
        //use the third parameter, email_address, to do something useful
    }
};

Below, token is a mongoose model. As I said, save takes a callback and gets passed error and model, but I'd like to also send my callback a variable email_address that I figure out at some other point. Obviously the appendParameter function is pseudo-code, but this is the type of functionality that I need.

token.save(saveCallBack.appendParameter(email_address));

If you make that the first parameter instead, you can use .bind() .

token.save(saveCallBack.bind(null, email_address));

var saveCallBack = function(email_address, err, model){};

I'm using bind function for appending additional parameters for callbackes

var customBind = function (fn, scope, args, appendArgs) {
            if (arguments.length === 2) {
                return function () {
                    return fn.apply(scope, arguments);
                };
            }

            var method = fn,
                slice = Array.prototype.slice;

            return function () {
                var callArgs = args || arguments;

                if (appendArgs === true) {
                    callArgs = slice.call(arguments, 0);
                    callArgs = callArgs.concat(args);
                } else if (typeof appendArgs == 'number') {
                    callArgs = slice.call(arguments, 0);
                }

                return method.apply(scope || window, callArgs);
            };
        }

This customBind function accepts four arguments, first one is original callback function, second is the scope, third is additional parameters (array), and fourth is flag append or replace. If you set last parameter to false than only parameters in array will be available in this function. and with this function you can simple add new parameters or to override the existing one

var callback = customBind(saveCallBack, this, [array_of_additional_params], true)

in this way all original parameters remain and your parameter will be appended to the end.

No matter how many parameter you defined, the callee will always pass the same parameter inside its process. but it will be more simple, just use a variable that is visible from outside of the callback. Eg:

var email = 'yourmail@mail.com';

var saveCallBack = function(err, model){
    if(err) {
        //handle error
    }

    else {
        alert(email);
    }
};

Updated (@Jason): then you can use Immediately-Invoked Function Expression (IIFE)

   (function(mail){
        var saveCallBack = function(err, model){
            if(err) {
                //handle error
            }

            else {
                alert(mail);
            }
        };
        token.save(saveCallBack);
    }, emailAddress);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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