Monday, June 3, 2024
129
rated 0 times [  131] [ 2]  / answers: 1 / hits: 23837  / 10 Years ago, fri, october 10, 2014, 12:00:00

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?


More From » asynchronous

 Answers
1

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.


[#69169] Wednesday, October 8, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
yulissamirandah

Total Points: 493
Total Questions: 115
Total Answers: 94

Location: Lebanon
Member since Sun, Aug 2, 2020
4 Years ago
yulissamirandah questions
Fri, Jun 17, 22, 00:00, 2 Years ago
Wed, Aug 26, 20, 00:00, 4 Years ago
Tue, Jan 28, 20, 00:00, 4 Years ago
Mon, Nov 11, 19, 00:00, 5 Years ago
;