TypeScript
Concurrent Tasks
Execute multiple tasks concurrently and gather results.
When building resilient applications, you often need to perform multiple operations in parallel to improve performance and user experience. Restate provides durable concurrency primitives that allow you to run tasks concurrently while maintaining deterministic execution during replays.
When to use concurrent tasks
Use concurrent tasks when you need to:
- Call multiple external services simultaneously (e.g., fetching data from different APIs)
- Race multiple operations and use the first result (e.g., trying multiple LLM providers)
- Implement timeouts by racing an operation against a timer
- Perform batch operations where individual tasks can run in parallel
Key benefits
- Deterministic replay: Restate logs the order of completion, ensuring consistent behavior during failures
- Fault tolerance: If your handler fails, tasks that were already completed will be replayed with their results, while pending tasks will be retried
Parallelizing tasks
Start multiple durable operations concurrently by calling them without immediately awaiting:
Check out the guide on parallelizing work.
Retrieving results
Restate provides several patterns for coordinating concurrent tasks. All patterns use RestatePromise combinators that log the order of completion, ensuring deterministic behavior during replays.
Wait for all tasks to complete
Similar to Promise.all, waits for all promises to resolve successfully:
Wait for the first successful completion
Similar to Promise.any, returns the first promise that resolves successfully:
Wait for the first to complete
Similar to Promise.race, returns the first promise to settle (resolve or reject):
Wait for all to settle
Similar to Promise.allSettled, waits for all promises to complete regardless of success or failure:
Creating already-completed promises
You can create RestatePromise instances that are already resolved or rejected, mirroring Promise.resolve and Promise.reject:
These are useful when you want to return an already-known value from a function that returns a RestatePromise.
Detecting RestatePromises
Use isRestatePromise to check whether a value is a RestatePromise before using it with combinators like RestatePromise.all():
Scheduling & Timers
Error Handling