// Real world
r12
Fetch with retry
A request that can succeed, fail, or be retried — without a tangle of nested if-statements. The graph names every legal transition; the calling code stays flat.
Code
const req = sm`
idle 'fetch' → loading;
loading 'resolve' → success;
loading 'reject' → error;
error 'retry' → loading;
success 'reset' → idle;
`;
async function run() {
req.go('fetch');
try { const data = await api.get(); req.go('resolve', { data }); }
catch { req.go('reject'); }
}