Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
How to affect the order of subscription callbacks in ReSwift/Redux

Affect the order/priority of subscription callbacks in ReSwift

(Reply to "Any way to control the priority of subscription callbacks?"

ReSwift does not guarantee any subscription callback order. Since ReSwift uses an unordered Set as its subscription store, there's no reliable way to know when a subscription is invoked.

This implementation detail is hidden by design. If you find yourself wanting to affect the order of subscription callbacks, there's a concept of a sequence waiting to be extracted somehow.

These are the ways out:

  1. Use object delegation. Have 1 "wrapper" subscriber forward the event to multiple other objects which are themselves not subscribers; the subscriber encapsulates the sequencing.
  2. Use event sequences (e.g. enum cases) with multiple subscribers each listening to different substates
  3. Use Middlewares if you want a side effect to happen before/after an action

Wrapper approach

Encapsulate the sequence in Subscriber and delegate to the Network service first, then to the Presenter afterwards:

|   State   |
| Subscriber|
   1) |                |  2)
+-----v-----+   +------v------+
| Network   |   |  Presenter  |
| service   |   |             |
+-----------+   +-------------+

Multiple states

Encapsulate the sequence in the state itself. A NetworkRequest enum with multiple states does this for you:

  1. .starting(URL)
  2. .pending(RequestInfo)
  3. .completed(Result)

Then you can have multiple subscribers waiting for the value to change to e.g. .starting to fire off the request.


The order of Middleware is set by the user. Listen for an action in your Middleware, then dispatch a network request from there.

You can also swallow or hold on to actions in the meantime, waiting for the request to complete. The Middleware may use internal caches of actions from request start to completion and to debounce repetetive dispatches, for example.

As a rule of thumb, I use subscribers to change my views and the middleware to apply other kind of side effects. ---Daniel Martín

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.