title: How to avoid dispatching in the middle of a dispatch
body: <p>Within my Flux architected React application I am retrieving data from a store, and would like to create an action to request that information if it does not exist. However I am running into an error where the dispatcher is already dispatching. </p>
  
  <p>My desired code is something like:</p>
  
  <pre><code>getAll: function(options) {
    options = options || {};
    var key = JSON.stringify(options);
    var ratings = _data.ratings[key];
  
    if (!ratings) {
      RatingActions.fetchAll(options);
    }
  
    return ratings || [];
  }
  </code></pre>
  
  <p>However intermittently fails when the dispatcher is already dispatching an action, with the message <code>Invariant Violation: Dispatch.dispatch(...): Cannot dispatch in the middle of a dispatch.</code>. I am often making requests in response to a change in application state (eg date range). My component where I make the request, in response to a change event from the <code>AppStore</code> has the following:</p>
  
  <pre><code>getStateFromStores: function() {
    var dateOptions = {
      startDate: AppStore.getStartISOString(),
      endDate: AppStore.getEndISOString()
    };
  
    return {
      ratings: RatingStore.getAll(dateOptions),
    };
  },
  </code></pre>
  
  <p>I am aware that event chaining is a Flux antipattern, but I am unsure what architecture is better for retrieving data when it does not yet exist. Currently I am using this terrible hack:</p>
  
  <pre><code>getAll: function(options) {
    options = options || {};
    var key = JSON.stringify(options);
    var ratings = _data.ratings[key];
  
    if (!ratings) {
      setTimeout(function() {
        if (!RatingActions.dispatcher.isDispatching()) {
          RatingActions.fetchAll(options);
        }
      }, 0);
    }
  
    return ratings || [];
  },
  </code></pre>
  
  <p>What would be a better architecture, that avoids event chaining or the dispatcher error? Is this really event chaining? I just want to change the data based on the parameters the application has set.</p>
  
  <p>Thanks! </p>