apply.js 3.05 KB
Newer Older
Rosanny Sihombing's avatar
Rosanny Sihombing committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});

exports.default = function (fn, ...args) {
  return (...callArgs) => fn(...args, ...callArgs);
};

module.exports = exports["default"]; /**
                                      * 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 module:Utils
                                      * @method
                                      * @category Util
                                      * @param {Function} fn - 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.
                                      * @returns {Function} the partially-applied function
                                      * @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
                                      */