diff options
Diffstat (limited to 'lib/apply.js')
-rw-r--r-- | lib/apply.js | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/lib/apply.js b/lib/apply.js index 76b5602..0c3c33d 100644 --- a/lib/apply.js +++ b/lib/apply.js @@ -2,6 +2,49 @@ import rest from 'lodash/rest'; +/** + * Creates a continuation function with some arguments already applied. + * + * Useful as a shorthand when combined with other control flow functions. Any + * arguments passed to the returned function are added to the arguments + * originally passed to apply. + * + * @name apply + * @static + * @memberOf async + * @category Util + * @param {Function} function - The function you want to eventually apply all + * arguments to. Invokes with (arguments...). + * @param {...*} arguments... - Any number of arguments to automatically apply + * when the continuation is called. + * @example + * + * // using apply + * async.parallel([ + * async.apply(fs.writeFile, 'testfile1', 'test1'), + * async.apply(fs.writeFile, 'testfile2', 'test2') + * ]); + * + * + * // the same process without using apply + * async.parallel([ + * function(callback) { + * fs.writeFile('testfile1', 'test1', callback); + * }, + * function(callback) { + * fs.writeFile('testfile2', 'test2', callback); + * } + * ]); + * + * // It's possible to pass any number of additional arguments when calling the + * // continuation: + * + * node> var fn = async.apply(sys.puts, 'one'); + * node> fn('two', 'three'); + * one + * two + * three + */ export default rest(function(fn, args) { return rest(function(callArgs) { return fn.apply(null, args.concat(callArgs)); |