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 8577989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:17:07+00:00 2026-06-11T20:17:07+00:00

I am invoking an API function – which takes a callback – multiple times

  • 0

I am invoking an API function – which takes a callback – multiple times in a for-loop. My callback needs to do something index-specific. I can’t change the willCallBack function (it is part of the API) and I want to avoid creating global variables.

The following snippet illustrates the problem. The first for-loop was my first attempt, but it returns i==4 for all callbacks. The second for-loop works – but it is ugly and feels ‘hackish’

What is a cleaner way of getting the value of ‘i’ into the callback function definition?

var result = '', result2 = '';

// doesn't work; i == 4 for all
for(var i=0; i<4; i++) {
    willCallBack(function(msg) {
        result += msg + i + '\n';
    });
}

// works, but kinda ugly
for(var i=0; i<4; i++) {
    willCallBack(function(i) {
        return function(msg) {
            result2 += msg + i + '\n';
        };
    }(i));
}

// part of API, cant change
function willCallBack(cb) {
    window.setTimeout(cb, 500, "can't change me");
}

// show the results
window.setTimeout(function(){
        alert(result + '\n\n' + result2)
    }, 1000);
  • 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-11T20:17:08+00:00Added an answer on June 11, 2026 at 8:17 pm

    The alternative to your “kinda ugly” version is to have a named function return the callback, instead of an anonymous, self-executing function. Maybe you’ll find it less ugly?

    for(var i=0; i<4; i++) {
        willCallBack(createCallback(i));
    }
    
    function createCallback(index) {
        return function(msg) {
            result2 += msg + index + '\n';
        };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am invoking a web service function which is reading a users credentials from
I want to write a C++ function that takes an llvm::Module , which is
When invoking a WCF service asynchronous there seems to be two ways it can
I have a method invoking bean which calls a method to perform some sort
This is how I am invoking the click : $(#myDivId).click(function(e) { callAjax(); window.location =
Instead of invoking a function on each matching value, I'd like get the array
I'm invoking a C++ function from within C#. This is the function header in
I'm invoking a REST service via http which returns a stream as the response.
I have taken a look at the PMD api which I want to use
I'm trying to reuse an existing database connection so that I can do multiple

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.