toast.promise() function automatically manages toast states based on a Promise’s lifecycle. This is perfect for async operations like API calls, file uploads, or data fetching.
How It Works
When you pass a Promise totoast.promise(), it:
- Immediately shows a loading toast
- Updates to a success toast when the Promise resolves
- Updates to an error toast if the Promise rejects
Basic Usage
Implementation Details
Here’s howtoast.promise() works under the hood (from src/core/toast.ts:58-85):
- Creates a loading toast with a unique ID
- Updates the same toast ID on success or error
- Returns the original Promise (doesn’t affect Promise chain)
Dynamic Messages
You can use functions to create dynamic messages based on the resolved or rejected value:Success with Response Data
Error with Details
Practical Examples
API Request
File Upload with Progress
Data Fetching
With Custom Options
Pass additional toast options as the third argument:Best Practices
Always return the Promise
Always return the Promise
toast.promise() returns the original Promise, so you can still use .then() and .catch() for additional logic:Provide clear messages
Provide clear messages
Make your messages descriptive and user-friendly:
Use dynamic messages for context
Use dynamic messages for context
Leverage the response data to provide specific feedback: