Version 1
[yaffs-website] / node_modules / grunt-legacy-util / node_modules / lodash / after.js
1 var toInteger = require('./toInteger');
2
3 /** Used as the `TypeError` message for "Functions" methods. */
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  * @category Function
13  * @param {number} n The number of calls before `func` is invoked.
14  * @param {Function} func The function to restrict.
15  * @returns {Function} Returns the new restricted function.
16  * @example
17  *
18  * var saves = ['profile', 'settings'];
19  *
20  * var done = _.after(saves.length, function() {
21  *   console.log('done saving!');
22  * });
23  *
24  * _.forEach(saves, function(type) {
25  *   asyncSave({ 'type': type, 'complete': done });
26  * });
27  * // => logs 'done saving!' after the two async saves have completed
28  */
29 function after(n, func) {
30   if (typeof func != 'function') {
31     throw new TypeError(FUNC_ERROR_TEXT);
32   }
33   n = toInteger(n);
34   return function() {
35     if (--n < 1) {
36       return func.apply(this, arguments);
37     }
38   };
39 }
40
41 module.exports = after;