Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80742 views
1
/** Used as the `TypeError` message for "Functions" methods. */
2
var FUNC_ERROR_TEXT = 'Expected a function';
3
4
/* Native method references for those with the same name as other `lodash` methods. */
5
var nativeIsFinite = global.isFinite;
6
7
/**
8
* The opposite of `_.before`; this method creates a function that invokes
9
* `func` once it is called `n` or more times.
10
*
11
* @static
12
* @memberOf _
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
if (typeof n == 'function') {
33
var temp = n;
34
n = func;
35
func = temp;
36
} else {
37
throw new TypeError(FUNC_ERROR_TEXT);
38
}
39
}
40
n = nativeIsFinite(n = +n) ? n : 0;
41
return function() {
42
if (--n < 1) {
43
return func.apply(this, arguments);
44
}
45
};
46
}
47
48
module.exports = after;
49
50