@@ -9,6 +9,7 @@ import { DefinitionType } from '../endpointDefinitions'
9
9
import type { QueryThunk , MutationThunk } from './buildThunks'
10
10
import type { AnyAction , ThunkAction , SerializedError } from '@reduxjs/toolkit'
11
11
import type { SubscriptionOptions , RootState } from './apiState'
12
+ import { QueryStatus } from './apiState'
12
13
import type { InternalSerializeQueryArgs } from '../defaultSerializeQueryArgs'
13
14
import type { Api , ApiContext } from '../apiTypes'
14
15
import type { ApiEndpointQuery } from './module'
@@ -274,17 +275,27 @@ Features like automatic cache collection, automatic refetching etc. will not be
274
275
originalArgs : arg ,
275
276
queryCacheKey,
276
277
} )
278
+ const selector = (
279
+ api . endpoints [ endpointName ] as ApiEndpointQuery < any , any >
280
+ ) . select ( arg )
281
+
277
282
const thunkResult = dispatch ( thunk )
283
+ const stateAfter = selector ( getState ( ) )
284
+
278
285
middlewareWarning ( getState )
279
286
280
287
const { requestId, abort } = thunkResult
281
288
289
+ const skippedSynchronously = stateAfter . requestId !== requestId
290
+
291
+ const runningQuery = runningQueries [ queryCacheKey ]
292
+
282
293
const statePromise : QueryActionCreatorResult < any > = Object . assign (
283
- Promise . all ( [ runningQueries [ queryCacheKey ] , thunkResult ] ) . then ( ( ) =>
284
- ( api . endpoints [ endpointName ] as ApiEndpointQuery < any , any > ) . select (
285
- arg
286
- ) ( getState ( ) )
287
- ) ,
294
+ skippedSynchronously && ! runningQuery
295
+ ? Promise . resolve ( stateAfter )
296
+ : Promise . all ( [ runningQuery , thunkResult ] ) . then ( ( ) =>
297
+ selector ( getState ( ) )
298
+ ) ,
288
299
{
289
300
arg,
290
301
requestId,
@@ -328,7 +339,7 @@ Features like automatic cache collection, automatic refetching etc. will not be
328
339
}
329
340
)
330
341
331
- if ( ! runningQueries [ queryCacheKey ] ) {
342
+ if ( ! runningQuery && ! skippedSynchronously ) {
332
343
runningQueries [ queryCacheKey ] = statePromise
333
344
statePromise . then ( ( ) => {
334
345
delete runningQueries [ queryCacheKey ]
0 commit comments