Skip to content
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
18 changes: 16 additions & 2 deletions src/mutation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,12 @@ const mutation = (<Data, Error>() =>
startTransition(() =>
setState({ data, isMutating: false, error: undefined })
)
options.onSuccess?.(data as Data, serializedKey, options)
// Await onSuccess to ensure async operations complete before returning
try {
await options.onSuccess?.(data as Data, serializedKey, options)
} catch (error) {
console.error('[SWR] onSuccess callback error:', error)
}
}
return data
} catch (error) {
Expand All @@ -96,7 +101,16 @@ const mutation = (<Data, Error>() =>
startTransition(() =>
setState({ error: error as Error, isMutating: false })
)
options.onError?.(error as Error, serializedKey, options)
// Await onError to ensure async operations complete
// Wrap in try-catch to prevent nested error handling
try {
await options.onError?.(error as Error, serializedKey, options)
} catch (nestedError) {
// onError callback errors should not interfere with error handling
if (typeof console !== 'undefined' && console.error) {
console.error('[SWR] onError callback error:', nestedError)
}
}
if (options.throwOnError) {
throw error as Error
}
Expand Down