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?
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?
Share Improve this question edited Oct 10, 2014 at 11:57 Adrien Cadet asked Oct 10, 2014 at 11:36 Adrien CadetAdrien Cadet 1,3412 gold badges15 silver badges21 bronze badges 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. – Jared Smith 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. – Paul S. 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. – Jared Smith Commented Oct 10, 2014 at 11:53
3 Answers
Reset to default 6If 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.
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.
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.