@@ -32,35 +32,43 @@ export function createAsyncThunk<
32
32
) {
33
33
const fulfilled = createAction (
34
34
type + '/fulfilled' ,
35
- ( result : Returned , args : ActionParams ) => {
35
+ ( result : Returned , args : ActionParams , requestId : string ) => {
36
36
return {
37
37
payload : result ,
38
- meta : { args }
38
+ meta : { args, requestId }
39
39
}
40
40
}
41
41
)
42
42
43
- const pending = createAction ( type + '/pending' , ( args : ActionParams ) => {
44
- return {
45
- payload : undefined ,
46
- meta : { args }
43
+ let requestIdCounter = 0
44
+
45
+ const pending = createAction (
46
+ type + '/pending' ,
47
+ ( args : ActionParams , requestId : string ) => {
48
+ return {
49
+ payload : undefined ,
50
+ meta : { args, requestId }
51
+ }
47
52
}
48
- } )
53
+ )
49
54
50
- const finished = createAction ( type + '/finished' , ( args : ActionParams ) => {
51
- return {
52
- payload : undefined ,
53
- meta : { args }
55
+ const finished = createAction (
56
+ type + '/finished' ,
57
+ ( args : ActionParams , requestId : string ) => {
58
+ return {
59
+ payload : undefined ,
60
+ meta : { args, requestId }
61
+ }
54
62
}
55
- } )
63
+ )
56
64
57
65
const rejected = createAction (
58
66
type + '/rejected' ,
59
- ( error : Error , args : ActionParams ) => {
67
+ ( error : Error , args : ActionParams , requestId : string ) => {
60
68
return {
61
69
payload : undefined ,
62
70
error,
63
- meta : { args }
71
+ meta : { args, requestId }
64
72
}
65
73
}
66
74
)
@@ -71,8 +79,11 @@ export function createAsyncThunk<
71
79
getState : TA [ 'getState' ] ,
72
80
extra : TA [ 'extra' ]
73
81
) => {
82
+ requestIdCounter ++
83
+ const requestId = `${ requestIdCounter } `
84
+
74
85
try {
75
- dispatch ( pending ( args ) )
86
+ dispatch ( pending ( args , requestId ) )
76
87
// TODO Also ugly types
77
88
const result = ( await payloadCreator ( args , {
78
89
dispatch,
@@ -81,13 +92,13 @@ export function createAsyncThunk<
81
92
} as TA ) ) as Returned
82
93
83
94
// TODO How do we avoid errors in here from hitting the catch clause?
84
- return dispatch ( fulfilled ( result , args ) )
95
+ return dispatch ( fulfilled ( result , args , requestId ) )
85
96
} catch ( err ) {
86
97
// TODO Errors aren't serializable
87
- dispatch ( rejected ( err , args ) )
98
+ dispatch ( rejected ( err , args , requestId ) )
88
99
} finally {
89
100
// TODO IS there really a benefit from a "finished" action?
90
- dispatch ( finished ( args ) )
101
+ dispatch ( finished ( args , requestId ) )
91
102
}
92
103
}
93
104
}
0 commit comments