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

Allow awaiting toast.promise #462

Merged
merged 1 commit into from
Sep 10, 2024
Merged

Allow awaiting toast.promise #462

merged 1 commit into from
Sep 10, 2024

Conversation

ajmnz
Copy link
Contributor

@ajmnz ajmnz commented Jun 27, 2024

Issue:

Closes #339

What has been done:

toast.promise now returns an unwrap method. This method returns a new promise that resolves or rejects when the provided promise does. Made sure to do this in a non-breaking way:

  • The id is still returned.
  • If the provided promise rejects, it will only reject when using unwrap. (see basic.spec for a test on that)
const result = await toast
  .promise(myPromise, { success: "Loaded", error: "Error" })
  .unwrap();

// `result` is now the resolved value of `myPromise`. In order to get the `id` we
// just have to use it like before

const id = toast.promise(myPromise, { success: "Loaded", error: "Error" }); // id = 3
const result = await id.unwrap(); // result = `myPromise` resolved value

Screenshots/Videos:

N/A

Copy link

vercel bot commented Jun 27, 2024

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

Name Status Preview Comments Updated (UTC)
sonner ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 27, 2024 0:56am

@netspencer
Copy link

would love to see this merged

@panzerfausten
Copy link

Hey I actually need this. Could @emilkowalski merge it please?

@panzerfausten
Copy link

Here's my workaround in case someone faces the same issue. You need to wrap it under a promise and call resolve() reject() accordingly :

const sendNewCodeWrapper = async () =>{

     return new Promise((resolve,reject) =>{
         toast.promise(API.users.sendNewAccessCode(email), {
             loading: t('sendingAccessCode'),
             success: (data) => {
                 setTimeout(() => {
                 }, 1000);
                 resolve(data);
                 return t('accessCodeSent');
             },
             error: (ex) => {
                 setIsSendingCode(false);
                 reject(ex);
                 return (ex?.response?.data?.localized_error || ex?.message || t('accessCodeSentError'));
             },
             finally: () => {
             }
         });
     });

 }

@emilkowalski emilkowalski merged commit ee55353 into emilkowalski:main Sep 10, 2024
2 checks passed
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.

Awaiting toast.promise
4 participants