javascript - chaining functions with a function argument -
what i'm trying 'connect' array of functions function. in array of functions:
var functionarray = [functiona, functionb, functionc];
functiona(next)
should able invoke call function argument next
call functionb
.
the problem cannot use promises (it make lot easier, unfortunate), devised way this:
var functionarray = [functiona, functionb]; functionarray.push(functionc.bind(undefined, args)); (var = functionarray.length - 1; i-- > 0; ) { functionarray[i] = functionarray[i].bind(undefined, args, functionarray[i + 1]); } functionarray[0]();
my question whether or not there better way accomplish this, above function looks terribly hacky. maybe array.prototype.reduce?
why not use multidimensional array store function , it's arguments. can cleaned basic idea this:
var functionarray = [ { func: yourfunction, args: [1, 2] } ]; (var = 0 < functionarray.length; i++) { var func = functionarray[i].func; var args = functionarray[i].args; func.apply(this, args); }
Comments
Post a Comment