Skip to content

Commit

Permalink
Merge pull request #179 from n33pm/feat/promiseOptionalMsg
Browse files Browse the repository at this point in the history
feat(): promise optional success/error msg
  • Loading branch information
timolins authored Dec 21, 2024
2 parents 38e11d0 + 952c314 commit 25bd873
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions src/core/toast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,28 +62,42 @@ 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,
});
const successMessage = msgs.success
? resolveValue(msgs.success, p)
: undefined;

if (successMessage) {
toast.success(successMessage, {
id,
...opts,
...opts?.success,
});
} else {
toast.dismiss(id);
}
return p;
})
.catch((e) => {
toast.error(resolveValue(msgs.error, e), {
id,
...opts,
...opts?.error,
});
const errorMessage = msgs.error ? resolveValue(msgs.error, e) : undefined;

if (errorMessage) {
toast.error(errorMessage, {
id,
...opts,
...opts?.error,
});
} else {
toast.dismiss(id);
}
});

return promise;
Expand Down

0 comments on commit 25bd873

Please sign in to comment.