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

Added 'SafePromise' branded Promises for createAsyncThunk #4102

Merged
merged 5 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/toolkit/src/createAsyncThunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
Id,
IsAny,
IsUnknown,
SafePromise,
TypeGuard,
} from './tsHelpers'
import { nanoid } from './nanoid'
Expand Down Expand Up @@ -242,7 +243,7 @@ export type AsyncThunkAction<
dispatch: GetDispatch<ThunkApiConfig>,
getState: () => GetState<ThunkApiConfig>,
extra: GetExtra<ThunkApiConfig>
) => Promise<
) => SafePromise<
| ReturnType<AsyncThunkFulfilledActionCreator<Returned, ThunkArg>>
| ReturnType<AsyncThunkRejectedActionCreator<ThunkArg, ThunkApiConfig>>
> & {
Expand Down Expand Up @@ -676,7 +677,7 @@ export const createAsyncThunk = /* @__PURE__ */ (() => {
}
return finalAction
})()
return Object.assign(promise as Promise<any>, {
return Object.assign(promise as SafePromise<any>, {
abort,
requestId,
arg,
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,6 @@ export { combineSlices } from './combineSlices'

export type { WithSlice } from './combineSlices'

export type { ExtractDispatchExtensions as TSHelpersExtractDispatchExtensions } from './tsHelpers'
export type { ExtractDispatchExtensions as TSHelpersExtractDispatchExtensions, SafePromise } from './tsHelpers'

export { formatProdErrorMessage } from './formatProdErrorMessage'
8 changes: 8 additions & 0 deletions packages/toolkit/src/tsHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,11 @@ export type Tail<T extends any[]> = T extends [any, ...infer Tail]
: never

export type UnknownIfNonSpecific<T> = {} extends T ? unknown : T

/**
* A Promise that will never reject.
* @see https://github.com/reduxjs/redux-toolkit/issues/4101
*/
export type SafePromise<T> = Promise<T> & {
__brand: 'SafePromise'
EskiMojo14 marked this conversation as resolved.
Show resolved Hide resolved
}