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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:30:28+00:00 2026-06-18T09:30:28+00:00

I am using a third-party JavaScript library that has a certain long-running function (it

  • 0

I am using a third-party JavaScript library that has a certain long-running function (it involves web service calls over the network, etc). For simplicity, let’s say it takes one parameter, a callback function to call when the long-running operation completes, so let’s say we have the signature longRunningFunction(callback).

Unfortunately, the function does not accept a “context” parameter, so if I call longRunningFunction multiple times, when my callback is called I have no way of knowing which call resulted in which callback.

I found the following way to solve this, by using anonymous functions: Define a mycallback(context) function, and then do something like this every time I invoke the long-running operation:

uniqueContext = getUniqueContextFromSomewhere();
longRunningFunction(function() {mycallback(uniqueContext)});

This seems to work, but my question is whether or not this is guaranteed to work according to the JavaScript spec in all possible circumstances, given that the long-running operation may be executing on a different thread, callbacks to the various calls to longRunningFunction may come in any order, etc. So, is the solution I found valid?

  • 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-18T09:30:29+00:00Added an answer on June 18, 2026 at 9:30 am

    Assuming this code :

    function longRunningFunction( fn ) {
        window.setTimeout( fn, 1000 ); // just to make it async
    }
    
    function myCallback( context ) {
        /* ... */
    }
    

    Now if you have this :

    var uniqueContext = getUniqueContext( );
    longRunningFunction( function ( ) { myCallback( uniqueContext ); } );
    

    It is specified by the standard that the context parameter of myCallback will be uniqueContext at the moment where the callback will be fired. It can cause some issues when you try something like this in a loop (because you will erase uniqueContext at each iteration).

    If you do this :

    var uniqueContext1 = getUniqueContext1( );
    longRunningFunction( function ( ) { myCallback( uniqueContext1 ); } );
    
    var uniqueContext2 = getUniqueContext2( );
    longRunningFunction( function ( ) { myCallback( uniqueContext2 ); } );
    

    The callback using uniqueContext1 is garanteed to be called when the first longRunningFunction will end, and the one using uniqueContext2 when the second longRunningFunction will end (with the same restriction than before; if you overwrite uniqueContext1 or uniqueContext2 somewhere in your scope, the callback parameter will change too).

    Using bind allows you to avoid creating a closure (and the default previously mentioned). The following code is similar to the previous one, except that you will never be able to accidentally change the value of the parameter by overwriting the previous one :

    longRunningFunction( mycallback.bind( null, getUniqueContext1( ) ) );
    longRunningFunction( mycallback.bind( null, getUniqueContext2( ) ) );
    

    mycallback.bind( null, someParameter ) will return a function which, when called, will call mycallback with null as this (which will fallback to window like any regular function) and someParameter as first parameter.

    The other answer is using only the first parameter of bind because they use the this variable, but it is not required and you can safely use function parameters instead.

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

Sidebar

Related Questions

I'm using a third party library which has a blocking function, that is, it
I'm using a third party javascript that has given me a lot of need
I am using a third party library that provide some callbacks for a widget,
I am trying to minify a third-party JavaScript library using Google Closure Compiler, but
This is bit tough question. Im using third party library that will insert result
I am using a third party library that mandates an iframe with some id,
I know that its possible to reference third party JavaScript files on the web
I'm using capybara-webkit to test integration with a third party website (I need javascript).
We are using a Third-Party assembly that seems to be compiled as AnyCPU. However,
I am using some third party library to connect to a server via async

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.