Hey, this is an English text. I want you to translate it into the Tunisian dialect.…
- User
Hey, this is an English text. I want you to translate it into the Tunisian dialect. Please follow these steps:
- Translate step by step and highlight differences between Tunisian dialect and Modern Standard Arabic.
- Highlight sections you are confident translating into the Tunisian dialect. Translate this text into the Tunisian dialect, focusing on capturing the essence of the language nuances:
Q: Stop pending async function in JavaScript 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? A: 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. A: 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.
- Assistant