Skip to content

Added 'SafePromise' branded Promises for createAsyncThunk #4102

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

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
MutationActionCreatorResult and QueryActionCreatorResult too
  • Loading branch information
JoshuaKGoldberg committed Jan 20, 2024
commit 789d97ce88ae7298c4275172de59638f29434b5a
20 changes: 12 additions & 8 deletions packages/toolkit/src/query/core/buildInitiate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import type { QueryResultSelectorResult } from './buildSelectors'
import type { Dispatch } from 'redux'
import { isNotNullish } from '../utils/isNotNullish'
import { countObjectKeys } from '../utils/countObjectKeys'
import type { SafePromise } from '../../tsHelpers'
import { asSafePromise } from '../../tsHelpers'

declare module './module' {
export interface ApiEndpointQuery<
Expand Down Expand Up @@ -60,7 +62,7 @@ type StartQueryActionCreator<

export type QueryActionCreatorResult<
D extends QueryDefinition<any, any, any, any>
> = Promise<QueryResultSelectorResult<D>> & {
> = SafePromise<QueryResultSelectorResult<D>> & {
arg: QueryArgFrom<D>
requestId: string
subscriptionOptions: SubscriptionOptions | undefined
Expand Down Expand Up @@ -90,7 +92,7 @@ type StartMutationActionCreator<

export type MutationActionCreatorResult<
D extends MutationDefinition<any, any, any, any>
> = Promise<
> = SafePromise<
| { data: ResultTypeFrom<D> }
| {
error:
Expand Down Expand Up @@ -335,7 +337,7 @@ You must add the middleware for RTK-Query to function correctly!`
const selectFromState = () => selector(getState())

const statePromise: QueryActionCreatorResult<any> = Object.assign(
forceQueryFn
(forceQueryFn
? // a query has been forced (upsertQueryData)
// -> we want to resolve it once data has been written with the data that will be written
thunkResult.then(selectFromState)
Expand All @@ -345,7 +347,9 @@ You must add the middleware for RTK-Query to function correctly!`
Promise.resolve(stateAfter)
: // query just started or one is already in flight
// -> wait for the running query, then resolve with data from after that
Promise.all([runningQuery, thunkResult]).then(selectFromState),
Promise.all([runningQuery, thunkResult]).then(
selectFromState
)) as SafePromise<any>,
{
arg,
requestId,
Expand Down Expand Up @@ -421,10 +425,10 @@ You must add the middleware for RTK-Query to function correctly!`
const thunkResult = dispatch(thunk)
middlewareWarning(dispatch)
const { requestId, abort, unwrap } = thunkResult
const returnValuePromise = thunkResult
.unwrap()
.then((data) => ({ data }))
.catch((error) => ({ error }))
const returnValuePromise = asSafePromise(
thunkResult.unwrap().then((data) => ({ data })),
(error) => ({ error })
)

const reset = () => {
dispatch(removeMutationResult({ requestId, fixedCacheKey }))
Expand Down
10 changes: 10 additions & 0 deletions packages/toolkit/src/tsHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,13 @@ export type UnknownIfNonSpecific<T> = {} extends T ? unknown : T
export type SafePromise<T> = Promise<T> & {
__safetyBrand: 'SafePromise'
}

/**
* Properly wraps a Promise as a @link {SafePromise} with .catch(fallback).
*/
export function asSafePromise<Resolved, Rejected>(
promise: Promise<Resolved>,
fallback: (error: unknown) => Rejected
) {
return promise.catch(fallback) as SafePromise<Resolved | Rejected>
}