I need to know when all the work initiated by a function call is complete.
I have a function that takes another function as a parameter and calls on that function:
function outer(inner) {
inner()
}
The inner function may initiate some async behavior:
function inner() {
new Promise(...)
new Promise(...)
new Promise(...)
requestAnimationFrame(...)
requestAnimationFrame(...)
}
I know how Promise.all
works. But for Promise.all, you need to pass an array of promises. I want my function to be unaware of the details of what happens inside the inner function. Since javascript handles a good deal of asynchronous behavior with callbacks instead of promises, Promise.all cannot handle every case of asynchronous work.
From the scope of the outer function, is there any way to detect whether the async and sync work of the inner function is complete, and for this behavior to apply to any function that I pass into outer
?
function outer(inner) {
inner.allWorkComplete(() => ...do more work)
inner();
}
I am trying to avoid hardcoding this into the inner function, because that defeats the point of building a higher order function to handle the allWorkComplete
event.
Is this simply an anti-pattern in javascript?