6

Here I have two JS async functions running simultaneously.

When one has ended (callback has been run), I would like to stop other one to go ahead. However (that's my issue) I cannot use global vars. Then, I would like to know if it is possible to stop a pending function in JS or any way to solve my problem.

I will appreciate any answers :)

EDIT:

Some clarifications:

  • I am here using pure JS. No HTML provided.
  • When I am talking about asynchronous, it could be every async function, not only ajax (database, timeout etc.).
  • We do not know their runtime.

About code, here is a sample of what I would like to produce:

asyncFirst(
    // ... args
    function() { // callback
        console.log('foo');
        stopOther();
    }
);

asyncSecond(
    // ... args
    function() { // callback
        console.log('bar');
        stopOther();
    }
);

asyncFirst(...);
asyncSecond(...);

What algorithm for stopOther() without using 'state' vars?

3
  • Are these running in the same local scope? Do you have to go all the way to global to set a flag variable? It would help to see code. Commented Oct 10, 2014 at 11:41
  • Use an IIFE as an ancestor of this section of code to create a closure which will allow you to share variables across functions without polluting the global namespace. Commented Oct 10, 2014 at 11:47
  • That only works if the callbacks are already in the same scope (and if it isn't the global, no need for an IIFE) or can be re-factored to be so. What if they're in different modules? Need more info from the OP. Commented Oct 10, 2014 at 11:53

3 Answers 3

6

If these functions run in the same scope you can simply set a flag variable and use that as a guard at the end of each callback:

var flag = false;
function async1() {
    //check if other function is done
    if (!flag) {
        //do stuff
    }
    flag = true;
}

function async2() {
    //same structure as the first
}

If these are running in the global scope you will need to use Paul S.'s suggestion of an IIFE

(function() {
    var flag = false;
    function async1() {
        //check if other function is done, if it is don't bother
        if (!flag) {
            //do stuff
        }
        flag = true;
    }
    function async2() {
        //ditto
    }
})();

This will prevent pollution of the global namespace. Note that javascript being single threaded HELPS you here, because the two functions can't hit flag at the exact same time, only one of them will actually make changes. Note also that async1 && 2 do not need to be defined in the same scope, but flag will have to exist in the scope that they are called from.

Sign up to request clarification or add additional context in comments.

6 Comments

With a flag, I would have done the same method than yours :) But my issue is to do same stuff without this kind of var.
You mentioned that you couldn't use a global var. In the second example above flag is not global, it is local to the anonymous function that is invoked immediately after being declared. Not sure why this won't work for you, is there a reason other than global namespace pollution you can't use a flag?
Sorry, I was unclear. I meant using a flag var, as you explained. Actually, I don't know when async callback will be run. In my algorithm, I could use a flag as you did, but meanwhile async function has ended, flag could have been reset or edited. Then, I can assume nothing for it. That's why I am seeking an alternative.
In that case you have only one option that I know of. Javascript has 'run until done' execution with ONE exception, the upcoming (read: not yet implemented in most browsers) ES 6 spec introduces the yield operator which suspends function execution. You can make all your asyncs yield before modifying state and have an 'async manager' that restarts excution on which one you want. Check out kangax's compatibility tables to see which browsers it works in now.
I don't understand what you want to do. Could you post a short example please?
|
6

Unfortunately, there is no way to stop an asynchronous running method without using 'state' var. Once called, method cannot be stopped, even using very 'low-level' JS methods.

For 'state' var, please check Jared Smith's answer.

Comments

-3

Well, if you can't use global var, something as to tell you to not process ...

1) put something in a hidden field and check for that value.

2) make an ajax call to check a flag server side.

that the only thing i see.

3 Comments

There's no need for such hacks. JavaScript has local (private) variables.
That's ridiculous. There are plenty of other ways depending on what scope the callbacks are in, what 3rd party libs (if any) are in use, etc. No need to drop a nuclear bomb on the anthill.
i agree but he specified he cannot use global var so i tought the solution of using javascript variable client side is exclude

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.