Basically i have multiple components across the page which own scope and multiple services that fetches some data.
What is best practice for example if one late component which is initiated on some user action needs some conditions from state of other components and some multiple future fetches to be resolved.
For simple example on pseudo language :
component on click =>
if( data1.fetched && data2.fetched && component1.action1
&& component2.action2 ...)
do something ();
wait for it;
I need to point that there are situation that all of conditions are already resolved and their events emited before component initialized and attached to event listeners and in some situation all this conditions are not even started, in most situation there is combination, so component actino must be aware of past and future.
Currently I am achieving this simply with waiting function like this:
component on click =>
wait_for([data1.fetched && data2.fetched && component1.action1 && component2.action2],
callback do your staff)
wait_for(collection,callback) =>
collection.each => wait(item)
count = collection.count
finished = 0;
wait(item) =>
if(item)
respond(item)
else
setTimeout => wait(item), 1000
respond(item)
finished++
if(count == finished)
callback do your staff
Basically i can not do event listeners because some components are as i said initialized long after those emits fired but i dont know about Promises, what about them dealing with things that are already settled in past like done fetches and state changes.
In real use example lets say that we have component google map that is inserted on user action long after one of its dependency google maps library initialized for other component, but this one needs another dependency, for example google places api to fetch an also needs to wait for state parameter from component address field to emit some action ( but component for other reasons need to be initialized before that action )
Basically question is what is best practice for multiple async condition to be awaited to continue to some action on javascript and are there some library especially for this ( no jQuery, just library for this, like axios )