Handling promises
toast.loading
If you want to take care of each step yourself you can use toast.loading
and update the notification yourself.
toast.promise
The library exposes a toast.promise
function. Supply a promise or a function that return a promise and the notification will be updated if it resolves or fails. When the promise is pending a spinner is displayed.
Let's start with a simple example
TIP
toast.promise
return the provided promise so you can chain it
const response = await toast.promise(
fetch('A_URL'),
{
pending: 'Promise is pending',
success: 'Promise resolved 👌',
error: 'Promise rejected 🤯',
},
);
console.log(response);
Displaying a simple message is what you would want to do in 90% of cases. But what if the message you want to display depends on the promise response, what if you want to change some options for the error notification? Rest assured, under the hood, the library uses toast.update
. Thanks to this, you have full control over each notification.