How to check if there is already running function and if it is exist listen to this function result;
async function a() {
// wait 5 seconds and return foo = foo 1;
// if A is already running await and return result of this running function A instead result;
}
CodePudding user response:
Solution
You can create class that will be execute one flow (maximum) and await result if flow already running. It may looks something like that:
class OneThreadExecutor {
// Boolean variable which represents is task running right now
taskRunning = false;
// All Promise.resolve callbacks
listeners = [];
// Accept initial value
constructor(value = 0) {
this.value = value;
}
// Send [result = value 1] after 5 sec
startNewTask = () => {
this.taskRunning = true;
setTimeout(() => {
this.taskRunning = false;
return this.sendResult();
}, 5000)
}
// Call all Promise.resolve callbacks, and pass [value 1] to them
sendResult = () => {
for (const listener of this.listeners) {
listener( this.value);
}
this.listeners = [];
}
// Main method that exec one task
getResult = () => new Promise(resolve => {
// Add callback to queue
this.listeners.push(resolve);
// Start new task if necessary
if (!this.taskRunning) this.startNewTask();
})
}
General concept
Async getResult
method will register promise in class' queue. Any successfull task execution will send result to queue. Current task returns value 1
on each getResult
call and take 5 seconds for whole flow.
Usage
const a = new OneThreadExecutor(); // value = 0 by default
// will start task#1, 5 sec left till .then call, value = 1
a.getResult().then(...)
// 2.5 sec left, task#1 is already half progress, value = 2
setTimeout(() => {
a.getResult().then(...)
}, 2500)
Async/await
const a = new OneThreadExecutor(3); // let's set value = 3 initially
// will start task#1, 5 sec left till .then call, value = 4
a.getResult();
// wait till task#1 completed, 5 sec left, value = 5
const value = await a.getResult();
// will start task#2 bacause not task running, 5 sec left, value = 6
a.getResult();
Cons
In demo solution we already expect successful task execution, without error handling, so you may need to extend it for proper error catching during task execution.
CodePudding user response:
If I translate the problem correctly, A
returns a promise that is asynchronously settled. While the promise is pending, all calls to a wrapper function around A
should return the currently pending promise.
If, however, A has not been called, or a previously return promise has been settled, A should be called again.
This can be achieved by chaining off the promise returned by A, using promise handlers to determine results are no longer pending, and have the wrapper function return the chained promise. This example code speeds up the process a little - four successive calls made to a
500ms apart get the same fulfilled value from A
which is takes 2000ms to perform a mythical asynchronous task:
// promise a delay
const delay = ms => new Promise(resolve=>setTimeout(resolve, ms));
// async function A
let foo =0;
async function A() {
await delay( 2000); // 2 second example
return foo = foo 1;
}
// Wrapper function a
let a;
{ // block scoped
let promiseA;
a = ()=> {
return promiseA ? promiseA :
promiseA =
A().then(
data => { promiseA=null; return data },
err => { promiseA=null; throw err}
);
}
}
// and test
async function test() {
for( let i=1; i < 11; i) {
a().then( data=> console.log(`a() call ${i} fulfills with ${data}`));
await delay(500);
}
}
test();
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
- The code block enclosed by
{
and}
gives thelet
variable block scope and makes it invisible from outside. Other solutions are possible. - The
then
call uses the two parameter form for brevity. - The catch handler re-throws the error for caller code to handle.
- synchronous and asynchronous code don't mix well together. Calls to the wrapper function
a
always receive a promise value in return - although sometimes it's pending, sometimes not.
CodePudding user response:
var is_a_running = false;
function a() {
if (is_a_running == false) {
is_a_running = true
//do something
//after you have done the thing
is_a_running = false
}
else if (is_a_running == true){
result();
}
}
This is should help you