Skip to content

Commit 789d97c

Browse files
MutationActionCreatorResult and QueryActionCreatorResult too
1 parent bae0c01 commit 789d97c

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

packages/toolkit/src/query/core/buildInitiate.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import type { QueryResultSelectorResult } from './buildSelectors'
2121
import type { Dispatch } from 'redux'
2222
import { isNotNullish } from '../utils/isNotNullish'
2323
import { countObjectKeys } from '../utils/countObjectKeys'
24+
import type { SafePromise } from '../../tsHelpers'
25+
import { asSafePromise } from '../../tsHelpers'
2426

2527
declare module './module' {
2628
export interface ApiEndpointQuery<
@@ -60,7 +62,7 @@ type StartQueryActionCreator<
6062

6163
export type QueryActionCreatorResult<
6264
D extends QueryDefinition<any, any, any, any>
63-
> = Promise<QueryResultSelectorResult<D>> & {
65+
> = SafePromise<QueryResultSelectorResult<D>> & {
6466
arg: QueryArgFrom<D>
6567
requestId: string
6668
subscriptionOptions: SubscriptionOptions | undefined
@@ -90,7 +92,7 @@ type StartMutationActionCreator<
9092

9193
export type MutationActionCreatorResult<
9294
D extends MutationDefinition<any, any, any, any>
93-
> = Promise<
95+
> = SafePromise<
9496
| { data: ResultTypeFrom<D> }
9597
| {
9698
error:
@@ -335,7 +337,7 @@ You must add the middleware for RTK-Query to function correctly!`
335337
const selectFromState = () => selector(getState())
336338

337339
const statePromise: QueryActionCreatorResult<any> = Object.assign(
338-
forceQueryFn
340+
(forceQueryFn
339341
? // a query has been forced (upsertQueryData)
340342
// -> we want to resolve it once data has been written with the data that will be written
341343
thunkResult.then(selectFromState)
@@ -345,7 +347,9 @@ You must add the middleware for RTK-Query to function correctly!`
345347
Promise.resolve(stateAfter)
346348
: // query just started or one is already in flight
347349
// -> wait for the running query, then resolve with data from after that
348-
Promise.all([runningQuery, thunkResult]).then(selectFromState),
350+
Promise.all([runningQuery, thunkResult]).then(
351+
selectFromState
352+
)) as SafePromise<any>,
349353
{
350354
arg,
351355
requestId,
@@ -421,10 +425,10 @@ You must add the middleware for RTK-Query to function correctly!`
421425
const thunkResult = dispatch(thunk)
422426
middlewareWarning(dispatch)
423427
const { requestId, abort, unwrap } = thunkResult
424-
const returnValuePromise = thunkResult
425-
.unwrap()
426-
.then((data) => ({ data }))
427-
.catch((error) => ({ error }))
428+
const returnValuePromise = asSafePromise(
429+
thunkResult.unwrap().then((data) => ({ data })),
430+
(error) => ({ error })
431+
)
428432

429433
const reset = () => {
430434
dispatch(removeMutationResult({ requestId, fixedCacheKey }))

packages/toolkit/src/tsHelpers.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,13 @@ export type UnknownIfNonSpecific<T> = {} extends T ? unknown : T
215215
export type SafePromise<T> = Promise<T> & {
216216
__safetyBrand: 'SafePromise'
217217
}
218+
219+
/**
220+
* Properly wraps a Promise as a @link {SafePromise} with .catch(fallback).
221+
*/
222+
export function asSafePromise<Resolved, Rejected>(
223+
promise: Promise<Resolved>,
224+
fallback: (error: unknown) => Rejected
225+
) {
226+
return promise.catch(fallback) as SafePromise<Resolved | Rejected>
227+
}

0 commit comments

Comments
 (0)