@@ -104,83 +104,63 @@ export function createAsyncThunk<
104
104
function actionCreator ( args : ActionParams ) {
105
105
const abortController = new AbortController ( )
106
106
107
- let dispatchAbort :
108
- | undefined
109
- | ( ( reason : string ) => ReturnType < typeof rejected > )
110
-
111
- let abortReason : string
112
-
113
- function abort ( reason : string = 'Aborted.' ) {
114
- abortController . abort ( )
115
- if ( dispatchAbort ) {
116
- dispatchAbort ( reason )
117
- } else {
118
- abortReason = reason
119
- }
120
- }
121
-
122
- async function thunkAction (
107
+ return function thunkAction (
123
108
dispatch : TA [ 'dispatch' ] ,
124
109
getState : TA [ 'getState' ] ,
125
110
extra : TA [ 'extra' ]
126
111
) {
127
112
const requestId = nanoid ( )
128
113
let abortAction : ReturnType < typeof rejected > | undefined
129
- dispatchAbort = reason => {
114
+
115
+ function abort ( reason : string = 'Aborted.' ) {
116
+ abortController . abort ( )
130
117
abortAction = rejected (
131
118
new DOMException ( reason , 'AbortError' ) ,
132
119
requestId ,
133
120
args
134
121
)
135
122
dispatch ( abortAction )
136
- return abortAction
137
- }
138
- // if the thunkAction.abort() method has been called before the thunkAction was dispatched,
139
- // just dispatch an aborted-action and never start with the thunk
140
- if ( abortController . signal . aborted ) {
141
- return dispatchAbort ( abortReason )
142
123
}
143
124
144
- let finalAction : ReturnType < typeof fulfilled | typeof rejected >
145
- try {
146
- dispatch ( pending ( requestId , args ) )
147
-
148
- finalAction = fulfilled (
149
- await payloadCreator ( args , {
150
- dispatch,
151
- getState,
152
- extra,
125
+ const promise = ( async function ( ) {
126
+ let finalAction : ReturnType < typeof fulfilled | typeof rejected >
127
+ try {
128
+ dispatch ( pending ( requestId , args ) )
129
+
130
+ finalAction = fulfilled (
131
+ await payloadCreator ( args , {
132
+ dispatch,
133
+ getState,
134
+ extra,
135
+ requestId,
136
+ signal : abortController . signal
137
+ } as TA ) ,
153
138
requestId ,
154
- signal : abortController . signal
155
- } as TA ) ,
156
- requestId ,
157
- args
158
- )
159
- } catch ( err ) {
160
- if (
161
- err instanceof DOMException &&
162
- err . name === 'AbortError' &&
163
- abortAction
164
- ) {
165
- // abortAction has already been dispatched, no further action should be dispatched
166
- // by this thunk.
167
- // return a copy of the dispatched abortAction, but attach the AbortError to it.
168
- return Object . assign ( { } , abortAction , {
169
- error : miniSerializeError ( err )
170
- } )
139
+ args
140
+ )
141
+ } catch ( err ) {
142
+ if (
143
+ err instanceof DOMException &&
144
+ err . name === 'AbortError' &&
145
+ abortAction
146
+ ) {
147
+ // abortAction has already been dispatched, no further action should be dispatched
148
+ // by this thunk.
149
+ // return a copy of the dispatched abortAction, but attach the AbortError to it.
150
+ return { ...abortAction , error : miniSerializeError ( err ) }
151
+ }
152
+ finalAction = rejected ( err , requestId , args )
171
153
}
172
- finalAction = rejected ( err , requestId , args )
173
- }
174
154
175
- // We dispatch "success" _after_ the catch, to avoid having any errors
176
- // here get swallowed by the try/catch block,
177
- // per https://twitter.com/dan_abramov/status/770914221638942720
178
- // and https://redux-toolkit.js.org/tutorials/advanced-tutorial#async-error-handling-logic-in-thunks
179
- dispatch ( finalAction )
180
- return finalAction
155
+ // We dispatch "success" _after_ the catch, to avoid having any errors
156
+ // here get swallowed by the try/catch block,
157
+ // per https://twitter.com/dan_abramov/status/770914221638942720
158
+ // and https://redux-toolkit.js.org/tutorials/advanced-tutorial#async-error-handling-logic-in-thunks
159
+ dispatch ( finalAction )
160
+ return finalAction
161
+ } ) ( )
162
+ return Object . assign ( promise , { abort } )
181
163
}
182
-
183
- return Object . assign ( thunkAction , { abort } )
184
164
}
185
165
186
166
return Object . assign ( actionCreator , {
0 commit comments