after.js
 1  var toInteger = require('./toInteger');
 2  
 3  /** Error message constants. */
 4  var FUNC_ERROR_TEXT = 'Expected a function';
 5  
 6  /**
 7   * The opposite of `_.before`; this method creates a function that invokes
 8   * `func` once it's called `n` or more times.
 9   *
10   * @static
11   * @memberOf _
12   * @since 0.1.0
13   * @category Function
14   * @param {number} n The number of calls before `func` is invoked.
15   * @param {Function} func The function to restrict.
16   * @returns {Function} Returns the new restricted function.
17   * @example
18   *
19   * var saves = ['profile', 'settings'];
20   *
21   * var done = _.after(saves.length, function() {
22   *   console.log('done saving!');
23   * });
24   *
25   * _.forEach(saves, function(type) {
26   *   asyncSave({ 'type': type, 'complete': done });
27   * });
28   * // => Logs 'done saving!' after the two async saves have completed.
29   */
30  function after(n, func) {
31    if (typeof func != 'function') {
32      throw new TypeError(FUNC_ERROR_TEXT);
33    }
34    n = toInteger(n);
35    return function() {
36      if (--n < 1) {
37        return func.apply(this, arguments);
38      }
39    };
40  }
41  
42  module.exports = after;