Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7912087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:29:24+00:00 2026-06-03T13:29:24+00:00

I’m using UnderscoreJs with nodejs and have a need for the _.times() method. times()

  • 0

I’m using UnderscoreJs with nodejs and have a need for the _.times() method. times() will invoke a function X number of times

This works as expected, however I need to iterate in a series, instead of in parallel which this appears to be doing.

Any idea if there’s a way to use this in series w/ callback methods?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T13:29:27+00:00Added an answer on June 3, 2026 at 1:29 pm

    Given something like this:

    function f() {
        some_async_call({ callback: function(err, results) {...})
    }
    _(3).times(f);
    

    Then the three f calls will happen in series but the some_async_call calls won’t necessarily happen in series because they’re asynchronous.

    If you want to force your calls to run in series then you need to use the callback on the async call to launch the next one in the series:

    function f(times, step) {
        step = step || 0;
        some_async_call({
            callback: function(err, results) {
                // Do something with `err` and `results`...
                if(step < times)
                    f(times, step + 1);
            }
        });
    }
    f(3);
    

    That approach will execute the three some_async_calls in series but, alas, the initial f(3) will return immediately. One solution to that problem is, of course, another callback:

    function f(from_n, upto, and_finally) {
        some_async_call({
            callback: function(err, results) {
                // Do something with `err` and `results`...
                if(from_n < upto)
                    f(from_n + 1, upto, and_finally);
                else
                    and_finally();
            }
        });
    }
    f(0, 3, function() { console.log('all done') });
    

    Where does _.times in with all this? No where really. _.times is just a for loop:

    _.times = function(n, iterator, context) {
      for (var i = 0; i < n; i++) iterator.call(context, i);
    };
    

    _.times exists for completeness and to allow you to add for loop when using _.chain. You could probably shoe-horn it in if you really wanted to but you would be making a big ugly mess instead of simplifying your code.

    You could use 250R’s async idea but you’d have to build an array of three functions but _.range and _.map would be more appropriate for that than _.times:

    // Untested off the top of my head code...
    
    function f(callback) {
        some_async_call({
            callback: function(err, results) {
                // Deal with `err` and `results`...
                callback();
            }
        });
    }
    
    var three_fs = _(3).range().map(function() { return f });
    async.series(three_fs);
    

    But you still have to modify f to have a callback function and if you’re always calling f three times then:

    async.series([f, f, f]);
    

    might be better than dynamically building the array with _.range and _.map.

    The real lesson here is that once you get into asynchronous function calls, you end up implementing all your logic as callbacks calling callbacks calling callbacks, callbacks all the way down.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.