timolins / react-hot-toast

Smoking Hot React Notifications 🔥
https://react-hot-toast.com
MIT License
9.73k stars 323 forks source link

feat: add optional "finally" callback after promise completion #343

Open fellypsantos opened 7 months ago

fellypsantos commented 7 months ago

The objective of this simple code increment is to allow execute some code ONLY after promise.toast completes its execution.

In my scenario it's useful to control UI elements that I want to keep disabled while the promise is running, disabling a button to be more specific, and reenabling the button with the "finally" callback, no matter if the promise resolves or rejects.

Example code:

setIsCreatingTodo(true); // one UI button is disabled while todo is being created

toast.promise(createTodo()), {
  loading: 'Loading...',
  success: (createdTodo) => {
    gotoTodoViewer(createdTodo);
    return 'success';
  },
  error: err => {
    const error = err as Error;
    return error.message;
  },
  finally: () => {
    setIsCreatingTodo(false);  // the button can be reactivated 
  }
});
vercel[bot] commented 7 months ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
react-hot-toast ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 11, 2024 5:58am