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(): promise optional success/error msg #179

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions src/core/toast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,28 +63,32 @@ toast.promise = <T>(
promise: Promise<T>,
msgs: {
loading: Renderable;
success: ValueOrFunction<Renderable, T>;
error: ValueOrFunction<Renderable, any>;
success?: ValueOrFunction<Renderable, T>;
error?: ValueOrFunction<Renderable, any>;
},
opts?: DefaultToastOptions
) => {
const id = toast.loading(msgs.loading, { ...opts, ...opts?.loading });

promise
.then((p) => {
toast.success(resolveValue(msgs.success, p), {
id,
...opts,
...opts?.success,
});
if (msgs.success)
toast.success(resolveValue(msgs.success, p), {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existing ValueOrFunction allows for null as a return value, it also seems reasonable that if a user returns null from the success/error function it could skip the toast. This seems preferable since especially in the case of errors, whether to show an error may itself be conditional on the type of Error raised.

id,
...opts,
...opts?.success,
});
else toast.remove(id);
return p;
})
.catch((e) => {
toast.error(resolveValue(msgs.error, e), {
id,
...opts,
...opts?.error,
});
if (msgs.error)
toast.error(resolveValue(msgs.error, e), {
id,
...opts,
...opts?.error,
});
else toast.remove(id);
});

return promise;
Expand Down