You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First of all, thanks for the library which is really great and makes my applications look much better :)
I would like to use toast.promise to issue an error if a promise completes, but contains a response that is an error - that is Response.ok is false or response.status is not 200. At the moment I have something like this:
const wg_promise = my_api_call()
toast.promise(wg_promise,
{
pending: "Getting API call",
success: {
render(response: any) {
if(!response.data.ok) {
return `failed!`
} else {
return `finished!`
}
}
},
error: "failed to get API call!"
},
{ theme: "dark" }
)
which does at least say "failed" when the response is a 500, but it does it in a green "success" toast. How do I adjust this? I thought I could use toast,update, which the documentation mentions, but I can't figure out how to get the toastId from the toast.promise call.
The text was updated successfully, but these errors were encountered:
Wrap your API call so it throw when response.data.ok is not ok
// return a string when it resolve otherwise throw an errorasyncfunctionmyPromise(){try{constresponse=awaitmy_api_call();if(!response.data.ok)thrownewError('Failed!');return'finished!';}catch(error){throwerror;}}toast.promise(myPromise,{pending: 'Getting API call',success: {// data is a stringrender({ data }){returndata;}},error: {// data is an instance of Errorrender({data}){returndata.message}}},{theme: 'dark'});
Using toast.loading + toast.update
asyncfunctionmyPromise(){try{consttoastId=toast.loading("Getting API call")constresponse=awaitmy_api_call();if(!response.data.ok){thrownewError("Failed!")}else{toast.update(toastId,{render: ()=>"Finished",type: "success"})}return'finished!';}catch(error){toast.update(toastId,{render: ()=>`${error.message}`,type: "error"})}}
First of all, thanks for the library which is really great and makes my applications look much better :)
I would like to use
toast.promise
to issue an error if a promise completes, but contains a response that is an error - that isResponse.ok
is false orresponse.status
is not 200. At the moment I have something like this:which does at least say "failed" when the response is a 500, but it does it in a green "success" toast. How do I adjust this? I thought I could use
toast,update
, which the documentation mentions, but I can't figure out how to get thetoastId
from thetoast.promise
call.The text was updated successfully, but these errors were encountered: