# Loading toast: show progress, then success or error

A loading toast should become the result, not sit next to it. Wrap the work in `toast.promise`, or start with `toast.loading` and call `toast.update` with the same id, and one toast goes from loading to success or error.

## The short way: toast.promise

`toast.promise` shows a loading toast, waits, then changes it into a success or an error. It takes a promise or a function that returns one.

```ts
import { toast } from 'react-boing';

try {
  const invoice = await toast.promise(() => createInvoice(order), {
    loading: 'Creating invoice…',
    success: (inv) => `Invoice ${inv.number} created`,
    error: 'Could not create the invoice',
  });
  openInvoice(invoice);
} catch {
  // The toast already shows the error; nothing else to report here.
}
```

It returns your promise, so you get the value back and rejections still reach your code. Handle them as you would without the toast.

## More control: toast.loading and toast.update

When the result needs a description, a button or a different type than plain success or error, keep the id and update it yourself:

```ts
const id = toast.loading('Importing contacts…');

const result = await importContacts(file);
if (result.ok) {
  toast.update(id, { type: 'success', title: 'Contacts imported', description: `${result.count} added` });
} else {
  toast.update(id, { type: 'error', title: 'Import failed', description: result.error });
}
```

Changing the type resets the timer to that type’s usual length: the success closes after 5 seconds, the error stays until dismissed. See [update a toast](https://boing.aayushbharti.in/docs/#update).

## How do I show progress in a toast?

Pass `progress` as a number from `0` to `1`. A thin bar along the bottom edge fills as you update it, and `progress: null` removes it.

```ts
const id = toast.loading('Uploading video…', { progress: 0 });

await uploadInChunks(file, (sent, total) => {
  toast.update(id, { progress: sent / total });
});

toast.update(id, { type: 'success', title: 'Video uploaded', progress: null });
```

Progress updates are not read out by screen readers, even when they carry a new description, so you can update as often as you like without flooding them. The final type change is announced.

## How long does a loading toast stay?

Until you update or dismiss it. A loading toast has no timer, so it will not vanish halfway through a slow request. That also means a forgotten update leaves it spinning, so make sure every path of your code ends in `toast.update` or `toast.dismiss(id)`.

> **Note:** If a newer toast has taken over the one you started, your late `update` is ignored. A slow request cannot overwrite a message that arrived after it.

## Frequently asked questions

### When does a loading toast go away?

No. Loading toasts stay until you update them to another type or dismiss them. Once updated to success, the 5 second timer starts.

### What happens if the promise rejects?

The toast becomes an error with your error message, and `toast.promise` returns the rejected promise so your code can handle it too.

### Can the success message use the result?

Yes. Pass a function as `success` and it receives the resolved value. `error` can be a function of the error in the same way.

### Are progress updates announced to screen readers?

No. Progress changes stay quiet so frequent updates do not interrupt. A change of type is announced, and so is a new title or description sent without a progress value.

## Related guides

- [React toast notifications](https://boing.aayushbharti.in/react-toast/): One component and one function call.
- [Undo toast](https://boing.aayushbharti.in/undo-toast/): Act first, offer undo.
- [React toast animation](https://boing.aayushbharti.in/react-toast-animation/): Springs, morphing in place and reduced motion.
- [Boing docs](https://boing.aayushbharti.in/docs/): Setup, options, styling, sound and accessibility.

Source: https://boing.aayushbharti.in/loading-toast/
