Skip to content

Commit bbb455e

Browse files
authored
add requestId property to the promise returned by createAsyncThunk (#784)
1 parent f8d8969 commit bbb455e

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

etc/redux-toolkit.api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ export type AsyncThunkAction<Returned, ThunkArg, ThunkApiConfig extends AsyncThu
8181
condition: boolean;
8282
}, SerializedError>> & {
8383
abort(reason?: string): void;
84+
requestId: string;
85+
arg: ThunkArg;
8486
};
8587

8688
// @public

src/createAsyncThunk.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,12 @@ describe('createAsyncThunk', () => {
7474

7575
const thunkFunction = thunkActionCreator(args)
7676

77-
await thunkFunction(dispatch, () => {}, undefined)
77+
const thunkPromise = thunkFunction(dispatch, () => {}, undefined)
78+
79+
expect(thunkPromise.requestId).toBe(generatedRequestId)
80+
expect(thunkPromise.arg).toBe(args)
81+
82+
await thunkPromise
7883

7984
expect(passedArg).toBe(args)
8085

@@ -355,8 +360,9 @@ describe('createAsyncThunk with abortController', () => {
355360
message: 'AbortReason',
356361
name: 'AbortError'
357362
},
358-
meta: { aborted: true }
363+
meta: { aborted: true, requestId: promise.requestId }
359364
}
365+
360366
// abortedAction with reason is dispatched after test/pending is dispatched
361367
expect(store.getState()).toMatchObject([
362368
{},

src/createAsyncThunk.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ export type AsyncThunkAction<
162162
>
163163
> & {
164164
abort(reason?: string): void
165+
requestId: string
166+
arg: ThunkArg
165167
}
166168

167169
type AsyncThunkActionCreator<
@@ -448,7 +450,7 @@ If you want to use the AbortController to react to \`abort\` events, please cons
448450
}
449451
return finalAction
450452
})()
451-
return Object.assign(promise, { abort })
453+
return Object.assign(promise, { abort, requestId, arg })
452454
}
453455
}
454456

type-tests/files/createAsyncThunk.typetest.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ const defaultDispatch = (() => {}) as ThunkDispatch<{}, any, AnyAction>
3232
)
3333

3434
const promise = defaultDispatch(async(3))
35+
36+
expectType<string>(promise.requestId)
37+
expectType<number>(promise.arg)
38+
expectType<(reason?: string) => void>(promise.abort)
39+
3540
const result = await promise
3641

3742
if (async.fulfilled.match(result)) {

0 commit comments

Comments
 (0)