An await
able 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.
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");
// ...
}
// ...