Skip to content

Commit 5dc1d06

Browse files
authored
remove unneccessary optionalPromise (#1077)
1 parent c45f85b commit 5dc1d06

File tree

4 files changed

+25
-83
lines changed

4 files changed

+25
-83
lines changed

src/query/core/buildMiddleware/cacheLifecycle.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ import type { AnyAction } from 'redux'
33
import type { ThunkDispatch } from 'redux-thunk'
44
import type { BaseQueryFn } from '../../baseQueryTypes'
55
import { DefinitionType } from '../../endpointDefinitions'
6-
import {
7-
OptionalPromise,
8-
toOptionalPromise,
9-
} from '../../utils/toOptionalPromise'
106
import type { RootState } from '../apiState'
117
import type {
128
MutationResultSelectorResult,
@@ -89,8 +85,10 @@ declare module '../../endpointDefinitions' {
8985
* to prevent memory leaks.
9086
* You can just re-throw that error (or not handle it at all) -
9187
* it will be caught outside of `cacheEntryAdded`.
88+
*
89+
* If you don't interact with this promise, it will not throw.
9290
*/
93-
cacheDataLoaded: OptionalPromise<ResultType>
91+
cacheDataLoaded: Promise<ResultType>
9492
/**
9593
* Promise that allows you to wait for the point in time when the cache entry
9694
* has been removed from the cache, by not being used/subscribed to any more
@@ -254,16 +252,17 @@ export const build: SubMiddlewareBuilder = ({
254252
const cacheEntryRemoved = new Promise<void>((resolve) => {
255253
lifecycle.cacheEntryRemoved = resolve
256254
})
257-
const cacheDataLoaded = toOptionalPromise(
258-
Promise.race([
259-
new Promise<void>((resolve) => {
260-
lifecycle.valueResolved = resolve
261-
}),
262-
cacheEntryRemoved.then(() => {
263-
throw neverResolvedError
264-
}),
265-
])
266-
)
255+
const cacheDataLoaded = Promise.race([
256+
new Promise<void>((resolve) => {
257+
lifecycle.valueResolved = resolve
258+
}),
259+
cacheEntryRemoved.then(() => {
260+
throw neverResolvedError
261+
}),
262+
])
263+
// prevent uncaught promise rejections from happening.
264+
// if the original promise is used in any way, that will create a new promise that will throw again
265+
cacheDataLoaded.catch(() => {})
267266
lifecycleMap[queryCacheKey] = lifecycle
268267
const selector = (api.endpoints[endpointName] as any).select(
269268
endpointDefinition.type === DefinitionType.query

src/query/core/buildMiddleware/queryLifecycle.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import { isPending, isRejected, isFulfilled } from '@reduxjs/toolkit'
22
import { BaseQueryFn } from '../../baseQueryTypes'
33
import { DefinitionType } from '../../endpointDefinitions'
4-
import {
5-
OptionalPromise,
6-
toOptionalPromise,
7-
} from '../../utils/toOptionalPromise'
84
import { Recipe } from '../buildThunks'
95
import { SubMiddlewareBuilder } from './types'
106

@@ -14,12 +10,14 @@ declare module '../../endpointDefinitions' {
1410
export interface QueryLifecyclePromises<ResultType> {
1511
/**
1612
* Promise that will resolve with the (transformed) query result.
17-
13+
*
1814
* If the query fails, this promise will reject with the error.
1915
*
2016
* This allows you to `await` for the query to finish.
17+
*
18+
* If you don't interact with this promise, it will not throw.
2119
*/
22-
queryFulfilled: OptionalPromise<ResultType>
20+
queryFulfilled: Promise<ResultType>
2321
}
2422

2523
interface QueryExtraOptions<
@@ -99,12 +97,13 @@ export const build: SubMiddlewareBuilder = ({
9997
const onQueryStarted = endpointDefinition?.onQueryStarted
10098
if (onQueryStarted) {
10199
const lifecycle = {} as CacheLifecycle
102-
const queryFulfilled = toOptionalPromise(
103-
new Promise((resolve, reject) => {
104-
lifecycle.resolve = resolve
105-
lifecycle.reject = reject
106-
})
107-
)
100+
const queryFulfilled = new Promise((resolve, reject) => {
101+
lifecycle.resolve = resolve
102+
lifecycle.reject = reject
103+
})
104+
// prevent uncaught promise rejections from happening.
105+
// if the original promise is used in any way, that will create a new promise that will throw again
106+
queryFulfilled.catch(() => {})
108107
lifecycleMap[requestId] = lifecycle
109108
const selector = (api.endpoints[endpointName] as any).select(
110109
endpointDefinition.type === DefinitionType.query

src/query/tests/optionalPromise.test.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/query/utils/toOptionalPromise.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)