Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

fellypsantos
Copy link

@fellypsantos fellypsantos commented Feb 11, 2024

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 
  }
});

Copy link

vercel bot commented Feb 11, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant