Saturday, October 25, 2025
HomeLanguagesJavascriptUnderscore.js _.after() Function

Underscore.js _.after() Function

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("neveropen")
        }
        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:
 neveropen
 neveropen
 neveropen
 neveropen
 neveropen

Example 2:




<!DOCTYPE html>
<html>
  
<head>
    <script src=
    </script>
</head>
  
<body>
    <button id="button">button</button>
    <script>
        show = () => {
            console.log("neveropen")
        }
  
        // 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:
 neveropen
 neveropen
 neveropen
 neveropen
 neveropen
 neveropen
 neveropen
 neveropen

Here, you need to click on the button in order to see the output.

Reference: https://underscorejs.org/#after

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS