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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:49:44+00:00 2026-06-09T21:49:44+00:00

I am trying to wrap my head around how to make asynchronous programming work.

  • 0

I am trying to wrap my head around how to make asynchronous programming work.

In my current use-case, I have functions that get called potentially multiple times per second and they have callbacks that rely on multiple variables that could change in between them.

A simplified example: (using coffeescript for brevity)

doSomething = (requestor, thing, action, callback) ->
  thing.takeAction action, (result) ->
    # actually a lot of times this nests down even further
    requestor.report result
    callback result

If doSomething gets called multiple times with different data before thing.takeAction returns its result, I assume I can’t rely on requestor and callback still being the same things I need them to be.
Correct?

To circumvent this I’d need to somehow inject requestor and callback into takeAction’s callback.
Is that possible somehow?

I got the idea of doing something like

doSomething = (requestor, thing, action, callback) ->
  thing.takeAction action, (result, _requestor = requestor, _callback = callback) ->
    _requestor.report result
    _callback result

But that of course is just a CoffeeScript hack and doesn’t work at all.


By the way, I was trying to use the caolan/async module to help me with this, but the fact still remains that I often need more variables in the callbacks than async lets me provide.
Like:

doSomething = function(requestor, thing, action, callback) {
  // this might not need a waterfall, but imagine it would have nested further

  async.waterfall(
  [
    function(next) {
      thing.takeAction(action, function(result) {
        // How can I know that action is still the same?
        next(null, result);
      });
    }, 
    function(result, next) {
      requestor.report(result); // requestor still the same?
      next(null, result);
    }
  ],

  function(err, result) {
    callback(result); // callback still the same?
  });
}

It still leaves me with the same problem. So how do I do this?

Thank you for your time.

  • 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-09T21:49:46+00:00Added an answer on June 9, 2026 at 9:49 pm

    You need to separate the action object content from the action value itself. That is, there is some object in the memory, which is referenced by the action name in this specific context.

    For example,

    function test(action) {
        alert("Test1: " + action.value);
        setTimeout(function () { alert("Test2: " + action.value); }, 1000);
    }
    
    var action = { value: 1; };
    test(action);
    action = { value: 2 };
    alert("Action value outside: " + action.value);
    

    will alert “Test1: 1”, “Action value outside: 2” and “Test1: 1”. However, once you replace action = { value: 2 }; with action.value = 2, the last alert will change to “Test1: 2”.

    So, if your problem is that some fields of the action object are changed outside, just clone it in the first line of your doSomething. If your problem is that the reference to the object is changed outside, you should not worry, it won’t affect your doSomething in any way.

    Additionally, the subsequent calls to doSomething do not “override” the value of the action parameter in your callbacks, as it is closed over the specific call: en.wikipedia.org/wiki/Closure_(computer_science)

    For example,

    function test(action) {
        alert("Test1: " + action.value);
        setTimeout(function () { alert("Test2: " + action.value); }, 1000);
    }
    
    var action = { value: 1; };
    test(action);
    action = { value: 2 };
    test(action);
    

    will alert “Test1: 1”, “Test1: 2”, “Test2: 1” and “Test2: 2” (not “Test1: 1”, “Test1: 2”, “Test2: 2” and “Test2: 2” as you seem to fear).

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

Sidebar

Related Questions

Trying to wrap my head around JTA and have arbitrarily chosen Bitronix as the
I'm trying to wrap my head around Object Oriented programming. But I'm having some
Trying to wrap my head around the apple design scheme. I have a UIViewController
I'm trying to wrap my head around Javascript's function.apply(). You use it like this:
I'm trying to wrap my head around this to make the correct design decisions.
I am trying to wrap my head around object oriented programming. My understanding is
I'm still trying to wrap my head around iphone memory management. I have checked
I'm new to Android programming and trying to wrap my head around this just
I'm trying to wrap my head around a Model that allows for multiple checkbox
I am trying to wrap my head around some WPF specific stuff, and have

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.