Resilient Asynchronous Flow
IdentityFlow is built to manage long-running, asynchronous processes with resilience. It ensures that workflows can pause, wait for external events, handle errors, and resume reliably.
export default defineWorkflow( { name: 'order-processing' }, async (flow) => { // Each step is tracked and can be retried await flow.do('validate order', async () => { await validateOrder(flow.params); });
// Run steps in parallel using Promise.all// Note: The workflow waits until all promises in Promise.all resolve.// Handling errors with Promise.allSettled or cancellation with Promise.race// requires careful consideration within the IdentityFlow model.await Promise.all([flow.do('process payment', async () => {await processPayment(flow.params.payment);}),flow.do('check inventory', async () => {await checkInventory(flow.params.items);}),]);
// Handle conditional flowsif (flow.params.requiresShipping) {await flow.do('arrange shipping', async () => {await arrangeShipping(flow.params);});}});export default defineWorkflow( { name: 'payment-processing', // Default retries for all steps in this workflow defaults: { retries: { limit: 3, delay: '30 seconds', backoff: 'exponential' } } }, async (flow) => { try { await flow.do('process payment', async () => { await processPayment(flow.params); }); } catch (error) { flow.error('Payment failed', { error: error.message, code: error.code }); // Re-throwing fails the workflow after retries are exhausted throw error; } });Core Resilience Features:
- Stateful Execution: Workflows maintain their state across asynchronous operations, ensuring they resume exactly where they left off.
- Built-in Retries: Configure automatic retries for failed steps with customizable delays and backoff strategies.
- Explicit Error Handling: Use standard
try...catchblocks andflow.error()for robust error management and logging. - Parallel Processing: Safely execute multiple asynchronous tasks in parallel using
Promise.all.
These capabilities allow you to build complex, long-running automations that can withstand transient failures and operate reliably.
Dive deeper into Error Handling strategies.