Skip to content

flushPendingReactions()

Synchronously runs MobX reactions from the internal pendingReactions queue when they piled up during a batch (inBatch > 0, e.g. inside runInAction).

While a batch is open, MobX only enqueues reactions; this call temporarily resets the batch counter, drains the queue, and restores state—useful in tests and when you need side effects before leaving the action.

If there are no pending reactions, a reaction run is already in progress (isRunningReactions), or the iteration cap is hit (cycle guard), there is no extra work; when the cap is exceeded the queue is cleared, matching MobX's internal safety behavior.

Example:

ts
import { observable, reaction, runInAction } from "mobx";
import { flushPendingReactions } from "yummies/mobx";

const state = observable({ count: 0 });
const log: number[] = [];
reaction(() => state.count, (n) => log.push(n));

runInAction(() => {
  state.count = 1;
  flushPendingReactions();
});

// log === [1] — the reaction ran before the action finished

Released under the MIT License.