title: Flux: waitFor specific event
body: <p>I'm trying to understand how to resolve dependencies among stores. The problem is I have a comprehensive data tree, which need to be fetched from server with the chain of request that depends one on another. </p>
  
  <p><strong>PROBLEM:</strong> <code>waitFor</code> seams not to be supposed for async requests. Suppose next event chain:</p>
  
  <ol>
  <li>NEED_A (look at <code>StoreA</code>)</li>
  <li>NEED_B (look at <code>StoreB</code>) Here <code>StoreB</code> do <code>AppDispatcher.waitFor([StoreA.dispatchToken])</code>. But actually we want to wait for <code>GET_A</code></li>
  <li>SOME_OTHER_ACTION (look at <code>StoreA</code>) </li>
  </ol>
  
  <p>The third step breaks <code>waitFor</code> from the second step since <code>StoreA.dispatchToken</code> was called for  <code>SOME_OTHER_ACTION</code>.</p>
  
  <p><strong>Question:</strong> <em>What is a true way to wait for some specific action (<code>GET_A</code>)?</em></p>
  
  <p>Let's take a look at the code (please pay attention to three <code>PROBLEM</code> comments):</p>
  
  <p><strong><em>StoreA</em></strong></p>
  
  <pre><code>var a = [];
  
  var StoreA = assign({}, EventEmitter.prototype, {
  
     getAProps: () =&gt; copyOfAProps(a);
  
     asyncGetA: () =&gt; ... //Async request returns Promise
  });
  
  StoreA.dispatchToken = AppDispatcher.register((action) =&gt; {
  
    switch(action.type) {
       NEED_A:
         StoreA.asyncGetA().then((data) =&gt; {             
           ActionCreator.getA(data); //Dispatches GET_A event
         });
         break;
       GET_A: 
         a = action.data;
         StoreA.emitChange();
       SOME_OTHER_ACTION: 
         //do whatever
    }
  
  });
  </code></pre>
  
  <p><strong><em>StoreB</em></strong></p>
  
  <pre><code>var b = [];
  
  var StoreB = assign({}, EventEmitter.prototype, {
  
     // PROBLEM: this request depends on data fetched from StoreA.asyncGetA
     asyncGetB: (A) =&gt; ...
  });
  
  StoreB.dispatchToken = AppDispatcher.register((action) =&gt; {
  
    switch(action.type) {
      //PROBLEM: NEED_B may happen before GET_A
      NEED_B:
        //PROBLEM: As I understand waitFor doesn't work here
        AppDispatcher.waitFor([StoreA.dispatchToken]);
        StoreB.asyncGetB(StoreA.getAProps()).then((data) =&gt; {
          ActionCreator.getB(data);
        });
      GET_B:
        b = action.data;
        StoreB.emitChange();
    }
  });
  </code></pre>