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

  • Home
  • SEARCH
  • 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 8069921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:13:21+00:00 2026-06-05T13:13:21+00:00

I hope this will make sense: I need to create a foreach function in

  • 0

I hope this will make sense:
I need to create a foreach function in javascript that will be used like this:

foreach(["A", "B", "C"], function(letter, done) {
    // do something async with 'letter'
    doSomthing(letter, done); // ***
}, function () {
    // final callback that is called after all array has been visted.
    // do some final work
});

So I was thinking about the following implementation:

var foreach = function(array, func, ready) {
    if (!array.length)
        ready();
    var that = this;
    func(array[0], function(){
        that.foreach(array.slice(1, array.length), func, ready);
    });
}

And it seems that it actually works! very cool.

But I was thinking if there is a solution that doesn’t use recursion?
I couldn’t think of one…

  • 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-05T13:13:24+00:00Added an answer on June 5, 2026 at 1:13 pm

    Your approach is technically correct but it is not good to do in such a way.
    Pls implement using promise pattern in javasript .
    I recommend you using when.js an open source js available on git for implementing promise pattern Pls refert to the below code

       var service = {
                fetch: function (query) {
    
                    // return a promise from the function
                    return when(["A", "B", "C"].forEach(function (name) {
                        alert(name);
                    }));
                }
            };
    
            service.fetch("hello world").then(function () {
                alert("work has been completed");
            });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hope this will be a quick easy question, I've just tried my app on
I hope this question will not be rejected as there is no code. But
Hope this is not a dupe. I would like to be able to do
Hope this makes sense... Is there a simple way to return a set of
I hope this is a quick question I hope. I need to write some
I'm trying to make a loop that will recurse through an array of bytes
I hope with this question that we can assemble a bunch of good advices
I hope that made sense, let me elaborate: There is a table of tracking
Ok so I hope this is a question that isn't to basic for you
Hope this is the right place to be asking this, so my apologies if

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.