fn
createAsyncMachine
testedCreate a type-safe async finite state machine with context
Example
ts
const machine = createAsyncMachine({
initial: 'idle',
context: { data: '' },
states: {
idle: {
on: {
FETCH: {
target: 'loaded',
guard: async () => await hasPermission(),
action: async (ctx) => { ctx.data = await fetchData(); },
},
},
},
loaded: {
entry: async (ctx) => { await saveToCache(ctx.data); },
},
},
});
await machine.send('FETCH'); // 'loaded'Signatures
ts
export function createAsyncMachine<
const States extends Record<string, AsyncStateNodeConfig<Context>>,
Context,
>(config: {
initial: NoInfer<ExtractStates<States>>;
context: Context;
states: States;
}): AsyncStateMachine<ExtractStates<States>, ExtractEvents<States>, Context>;ts
export function createAsyncMachine<
const States extends Record<string, AsyncStateNodeConfig<undefined>>,
>(config: {
initial: NoInfer<ExtractStates<States>>;
states: States;
}): AsyncStateMachine<ExtractStates<States>, ExtractEvents<States>, undefined>;Parameters
| Parameter | Type | Description |
|---|---|---|
config | { initial: string; context?: unknown; states: Record<string, AsyncStateNodeConfig<any>>; } | — |
Returns
AsyncStateMachine<string, string, undefined>