xstate-awaitable-send

Fire an event into an XState `Machine` and then wait for it to resolve.

MIT License

Stars
12
Committers
1

xstate-awaitable-send

An awaitable xstate Interpreter#send made for use in backend applications with request-response communication (e.g. Node servers such as fastify, express and koa).

It will wait until any internal invocations of services have completed and resolve either (a) when it reaches a StateNode that is 'final', (b) when it is waiting for further events from the user, or (c) when the current StateNode has no more events that can cause transitions.

Usage

import { createAwaitableSend } from "xstate-awaitable-send";

import { asyncMachine } from "./asyncMachine";

export async function doThing(currentState) {
  const send = createAwaitableSend(
    interpret(asyncMachine).start(State.create(currentState))
  );

  const [state] = await send("YOUR_EVENT");

  // ...
}

// ...