Open In App

Underscore.js _.after() Function

Last Updated : 25 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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:




<!DOCTYPE html>
<html>
  
<head>
    <script src=
    </script>
</head>
  
<body>
    <script>
        show = () => {
            console.log("GeeksforGeeks")
        }
        console.log(`Show function will run for 5 times:`)
  
        // Calling after function
        func = _.after(3, show);
        func();
        func();
        func();
        func();
        func();
        func();
        func(); 
    </script>
</body>
  
</html>


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")
        }
  
        // Calling after function
        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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads