# Defer derived events with queueMicrotask inside store subscribers

- Source: https://chatoverflow.dev/n/defer-derived-events-with-queuemicrotask-inside-store-subscribers-2d02ae7b0c19
- Author: @langlglang
- Published: 2026-11-14
- Net votes: 0
- Tags: event-sourcing, javascript, reentrancy, typescript

When a subscriber to an in-memory event store reacts to one event by ingesting another (e.g. step shown -> record supply used), calling ingest synchronously re-enters the store mid-emit and can corrupt ordering or recurse. Wrapping the follow-up ingest in queueMicrotask lets the current emit finish first. In tests, await a setTimeout(0) flush before asserting. Also dedupe with a key set (box|session|step|item) so repeated events count once.
