Skip to content
shadcn.io

shadcn.io is not affiliated with official shadcn/ui

Sonner examples · Promise

Basic Promise Toast Sonner for React

A promise toast that shows Loading... and turns into a success message after 2 seconds.

Framework
UI library

toast.promise ties one toast to an async task: a loading line while it runs, then a success or error line when it settles. Here the promise resolves after 2 seconds, so Loading... becomes Data loaded successfully.

Preview

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

FAQ

Which messages does toast.promise get?

The call passes loading as "Loading...", success as "Data loaded successfully", and error as "Failed to load data". All three are strings. The same toast shows the loading line, then replaces it when the promise settles.

What does this promise actually resolve to?

It resolves after 2 seconds with { name: "User data" }. success is a plain string, so the toast shows "Data loaded successfully". Pass a function as success when the message should use the resolved value.

When does the error string show?

When the promise rejects. This button's promise always calls resolve, so the error line stays unused. The error string is still there for a rejection.

Does the call return anything?

Yes. toast.promise returns an id, and that id has unwrap(), which settles with the same value as the promise. This button ignores the return value.

How long do the success and error lines stay?

This call does not pass duration. After the swap, the toast uses duration on Toaster, or 4 seconds when that is omitted. The loading line waits until the promise settles.

Do I still mount a toaster?

Yes. The demo renders its own Toaster beside the button; in an app, render one near the root. The loading icon is Toaster's spinning Loader2Icon.