Repository: reduxjs/redux-toolkit
Pull request #784 (2020-10-31 10:59:54)

add `requestId` property to the promise returned by `createAsyncThunk`
I noticed this to be lacking while experimenting with https://github.com/phryneas/rtk_simple-query - @markerikson is this okay with you?

netlify[bot]: Deploy preview for *redux-starter-kit-docs* ready!

Built with commit 6b8cc0bfbf029946b31f4ee759f2c8340a33c62c

https://deploy-preview-784--redux-starter-kit-docs.netlify.app
codesandbox[bot]: This pull request is automatically built and testable in [CodeSandbox](https://codesandbox.io).

  To see build info of the built libraries, click [here](https://ci.codesandbox.io/status/reduxjs/redux-toolkit/pr/784/builds/68535) or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 6b8cc0bfbf029946b31f4ee759f2c8340a33c62c:

|Sandbox| Source |
|--|--|
|[Vanilla](https://codesandbox.io/s/vanilla-44ets)| Configuration |
|[Vanilla Typescript](https://codesandbox.io/s/vanilla-typescript-7kj32)| Configuration |
|[rsk-github-issues-example](https://codesandbox.io/s/rsk-github-issues-example-k14d2)| Configuration |

phryneas: Hmm. I'd even go one step further and also add `arg` as a property of that promise. It's available and it's actually useful. Updating the PR.
markerikson: Sure, seems like it might be useful.

Patch:
diff --git a/etc/redux-toolkit.api.md b/etc/redux-toolkit.api.md
index 36429e42..207509fe 100644
--- a/etc/redux-toolkit.api.md
+++ b/etc/redux-toolkit.api.md
@@ -81,6 +81,8 @@ export type AsyncThunkAction<Returned, ThunkArg, ThunkApiConfig extends AsyncThu
     condition: boolean;
 }, SerializedError>> & {
     abort(reason?: string): void;
+    requestId: string;
+    arg: ThunkArg;
 };
 
 // @public
diff --git a/src/createAsyncThunk.ts b/src/createAsyncThunk.ts
index cab1d2c8..8650ba8f 100644
--- a/src/createAsyncThunk.ts
+++ b/src/createAsyncThunk.ts
@@ -162,6 +162,8 @@ export type AsyncThunkAction<
     >
 > & {
   abort(reason?: string): void
+  requestId: string
+  arg: ThunkArg
 }
 
 type AsyncThunkActionCreator<
@@ -448,7 +450,7 @@ If you want to use the AbortController to react to \`abort\` events, please cons
         }
         return finalAction
       })()
-      return Object.assign(promise, { abort })
+      return Object.assign(promise, { abort, requestId, arg })
     }
   }
 


Test patch:
diff --git a/src/createAsyncThunk.test.ts b/src/createAsyncThunk.test.ts
index 1f27cf59..359127bd 100644
--- a/src/createAsyncThunk.test.ts
+++ b/src/createAsyncThunk.test.ts
@@ -74,7 +74,12 @@ describe('createAsyncThunk', () => {
 
     const thunkFunction = thunkActionCreator(args)
 
-    await thunkFunction(dispatch, () => {}, undefined)
+    const thunkPromise = thunkFunction(dispatch, () => {}, undefined)
+
+    expect(thunkPromise.requestId).toBe(generatedRequestId)
+    expect(thunkPromise.arg).toBe(args)
+
+    await thunkPromise
 
     expect(passedArg).toBe(args)
 
@@ -355,8 +360,9 @@ describe('createAsyncThunk with abortController', () => {
         message: 'AbortReason',
         name: 'AbortError'
       },
-      meta: { aborted: true }
+      meta: { aborted: true, requestId: promise.requestId }
     }
+
     // abortedAction with reason is dispatched after test/pending is dispatched
     expect(store.getState()).toMatchObject([
       {},
diff --git a/type-tests/files/createAsyncThunk.typetest.ts b/type-tests/files/createAsyncThunk.typetest.ts
index 2695d6f7..6c344372 100644
--- a/type-tests/files/createAsyncThunk.typetest.ts
+++ b/type-tests/files/createAsyncThunk.typetest.ts
@@ -32,6 +32,11 @@ const defaultDispatch = (() => {}) as ThunkDispatch<{}, any, AnyAction>
   )
 
   const promise = defaultDispatch(async(3))
+
+  expectType<string>(promise.requestId)
+  expectType<number>(promise.arg)
+  expectType<(reason?: string) => void>(promise.abort)
+
   const result = await promise
 
   if (async.fulfilled.match(result)) {