Multiple asynchronous requests after dispatching
I've also run into some problems with the cannot dispatch in the middle of a dispatch error message.
My use case is as follows:
I load something from the server (a calendar object), the dispatcher dispatches an action once it has completed loading (UPDATE_CALENDAR). Now, I need to perform another call to the server if some attributes of the calender object specify a certain criteria.
So, prefreably, I would be able to call the server (once again using the ActionCreators) in response to my calendar object, but the problem is that my Calendar component is being updated in the CalendarStore's dispatch-token callback, so in effect the UPDATE_CALENDAR event is still being dispatched.
Is there a way to chain various dispatches? (The setTimeout hack works, but feels ugh.)
In essence, something like this:
ActionCreator -> (WebAPIUtils) -> ServerActionCreators ->
Dispatcher -> Store callbacks -> add new dispatch to be executed *after* the current dispatch finishes
Or perhaps some sort of after hook?
If this is unfeasable, is there a way to make components call an ActionCreator without user interaction / setTimeout?
Faced this too ... You can return Promise from action creators. This looks more clearer than setTimeout
You really don't want to chain dispatches. In your case, it sounds like you really want to do more work server-side and simply return the correct data. However, if you don't have control over the server-side code, then I can understand that you need to create a hack to make this work. In that case, yes, a Promise is the best strategy.
Keep in mind also that you don't have to use an action creator to create a call to the server. You simply need to handle the response with a new action.