Underscore.js _.after() Function (original) (raw)
Last Updated : 25 Nov, 2021
Underscore.js is a JavaScript library that provides a lot of useful functions that help in the programming in a big way like the map, filter, invoke, etc even without using any built-in objects.
The _.after() function is an inbuilt function in Underscore.js library of JavaScript which is used to create a wrapper of function which doesn’t do anything in the beginning but from the stated count it starts calling the stated function. Moreover, it is useful in a grouping of asynchronous responses, as you need to be sure if all the async calls have ended or not before preceding.
Syntax:
_.after(count, function)
Parameters: It accepts two parameters which are specified below:
- count: It is the number of counts after which the stated function will be called.
- function: It is the function stated.
Return values: This method returns the count of the function called.
Example 1:
Output:
Show function will run for 5 times: GeeksforGeeks GeeksforGeeks GeeksforGeeks GeeksforGeeks GeeksforGeeks
Example 2:
<!DOCTYPE html>
<html>
<head>
`` <script src=
`` </script>
</head>
<body>
`` <button id=
"button"
>button</button>
`` <script>
`` show = () => {
`` console.log(
"GeeksforGeeks"
)
`` }
`` func = _.after(3, show);
`` let button = document.querySelector(
"#button"
);
`` let Run = () => {
console.log(Show ``
function
runs
for `` 8 times:
) ``
`` for
(let i = 0; i < 10; i++) {
`` func();
`` }
`` }
`` button.addEventListener(
"click"
, Run)
`` </script>
</body>
</html>
Output:
button
Show function runs for 7 times: GeeksforGeeks GeeksforGeeks GeeksforGeeks GeeksforGeeks GeeksforGeeks GeeksforGeeks GeeksforGeeks GeeksforGeeks
Here, you need to click on the button in order to see the output.
Reference: https://underscorejs.org/#after
Similar Reads
- Underscore.js What is Underscore.js? Underscore.js is a lightweight JavaScript library and not a complete framework that was written by Jeremy Ashkenas. It provides utility functions for a variety of use cases in our day-to-day common programming tasks. Underscore.js provides a lot of features that make our task 4 min read
- Underscore.js Introduction Underscore.js is a lightweight JavaScript library and not a complete framework that was written by Jeremy Ashkenas that provides utility functions for a variety of use cases in our day to day common programming tasks. With just under six kilobytes in size, this library basically provides us with a w 2 min read