Skip to content

Commit b8ec4ce

Browse files
Added 'SafePromise' branded Promises for createAsyncThunk
1 parent 3c50985 commit b8ec4ce

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

packages/toolkit/src/createAsyncThunk.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type {
1111
Id,
1212
IsAny,
1313
IsUnknown,
14+
SafePromise,
1415
TypeGuard,
1516
} from './tsHelpers'
1617
import { nanoid } from './nanoid'
@@ -242,7 +243,7 @@ export type AsyncThunkAction<
242243
dispatch: GetDispatch<ThunkApiConfig>,
243244
getState: () => GetState<ThunkApiConfig>,
244245
extra: GetExtra<ThunkApiConfig>
245-
) => Promise<
246+
) => SafePromise<
246247
| ReturnType<AsyncThunkFulfilledActionCreator<Returned, ThunkArg>>
247248
| ReturnType<AsyncThunkRejectedActionCreator<ThunkArg, ThunkApiConfig>>
248249
> & {
@@ -676,7 +677,7 @@ export const createAsyncThunk = /* @__PURE__ */ (() => {
676677
}
677678
return finalAction
678679
})()
679-
return Object.assign(promise as Promise<any>, {
680+
return Object.assign(promise as SafePromise<any>, {
680681
abort,
681682
requestId,
682683
arg,

packages/toolkit/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,6 @@ export { combineSlices } from './combineSlices'
203203

204204
export type { WithSlice } from './combineSlices'
205205

206-
export type { ExtractDispatchExtensions as TSHelpersExtractDispatchExtensions } from './tsHelpers'
206+
export type { ExtractDispatchExtensions as TSHelpersExtractDispatchExtensions, SafePromise } from './tsHelpers'
207207

208208
export { formatProdErrorMessage } from './formatProdErrorMessage'

packages/toolkit/src/tsHelpers.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,11 @@ export type Tail<T extends any[]> = T extends [any, ...infer Tail]
207207
: never
208208

209209
export type UnknownIfNonSpecific<T> = {} extends T ? unknown : T
210+
211+
/**
212+
* A Promise that will never reject.
213+
* @see https://github.com/reduxjs/redux-toolkit/issues/4101
214+
*/
215+
export type SafePromise<T> = Promise<T> & {
216+
__brand: 'SafePromise'
217+
}

0 commit comments

Comments
 (0)