@@ -13,7 +13,7 @@ import {
13
13
IRequestResItem
14
14
} from './types'
15
15
16
- export function parseParams ( urlSearch : string , params ?: IAnyObject ) : string {
16
+ function parseParams ( urlSearch : string , params ?: IAnyObject ) : string {
17
17
let res = urlSearch ? `${ urlSearch } ` : '?'
18
18
19
19
if ( params ) {
@@ -28,7 +28,7 @@ export function parseParams(urlSearch: string, params?: IAnyObject): string {
28
28
return res
29
29
}
30
30
31
- export function parseHeaders (
31
+ function parseHeaders (
32
32
rawConfig : IRequestConfig ,
33
33
config : RequestOptions & IMapTypeEmptyObject < URL >
34
34
) {
@@ -47,7 +47,7 @@ export function parseHeaders(
47
47
return headers
48
48
}
49
49
50
- export function handleRequestConfig (
50
+ function handleRequestConfig (
51
51
rawConfig : IRequestConfig
52
52
) : RequestOptions & IMapTypeEmptyObject < URL > {
53
53
const { protocol, hostname, port, pathname, search } = new Url . URL (
@@ -77,6 +77,27 @@ export function handleRequestConfig(
77
77
return config
78
78
}
79
79
80
+ async function useSleepByBatch (
81
+ isHaveIntervalTime : boolean ,
82
+ isNumberIntervalTime : boolean ,
83
+ intervalTime : any ,
84
+ id : number
85
+ ) {
86
+ if ( isHaveIntervalTime && id > 1 ) {
87
+ const timeout : number = isNumberIntervalTime
88
+ ? intervalTime
89
+ : random ( intervalTime . max , intervalTime . min )
90
+
91
+ console . log (
92
+ `Request ${ id } needs to sleep for ${ timeout } milliseconds before sending`
93
+ )
94
+
95
+ await sleep ( timeout )
96
+ } else {
97
+ console . log ( `Request ${ id } does not need to sleep, send immediately` )
98
+ }
99
+ }
100
+
80
101
export function request ( config : IRequestConfig ) {
81
102
return new Promise < IRequest > ( ( resolve , reject ) => {
82
103
const isDataUndefine = isUndefined ( config . data )
@@ -122,47 +143,93 @@ export function request(config: IRequestConfig) {
122
143
123
144
export async function batchRequest (
124
145
requestConifgs : IRequestConfig [ ] ,
125
- intervalTime : IIntervalTime | undefined ,
126
- batchRequestResHandle : (
127
- error : Error | null ,
128
- requestResItem : IRequestResItem
129
- ) => void
146
+ intervalTime : IIntervalTime | undefined
130
147
) {
131
- const total = requestConifgs . length
132
- let id = 0
133
-
134
148
const isHaveIntervalTime = ! isUndefined ( intervalTime )
135
149
const isNumberIntervalTime = isNumber ( intervalTime )
136
150
137
- console . log ( `Begin execution, total: ${ total } ` )
151
+ console . log ( `Begin execution, mode: async, total: ${ requestConifgs . length } ` )
138
152
153
+ const requestQueue : Promise < IRequestResItem | string > [ ] = [ ]
154
+
155
+ let index = 0
139
156
for ( const requestConifg of requestConifgs ) {
140
- id ++
157
+ const id = ++ index
158
+
159
+ await useSleepByBatch (
160
+ isHaveIntervalTime ,
161
+ isNumberIntervalTime ,
162
+ intervalTime ,
163
+ id
164
+ )
165
+
166
+ const requestItem = request ( requestConifg )
167
+ . catch ( ( error : any ) => {
168
+ return `Request ${ id } is an error: ${ error . message } `
169
+ } )
170
+ . then ( ( requestRes ) => {
171
+ if ( typeof requestRes === 'string' ) return requestRes
141
172
142
- let state = 'success'
143
- let error : Error | null = null
173
+ return { id , ... requestRes }
174
+ } )
144
175
145
- let requestRes : IRequest = { } as IRequest
146
- try {
147
- requestRes = await request ( requestConifg )
148
- } catch ( err : any ) {
149
- error = err
150
- state = `error: ${ err . message } `
176
+ requestQueue . push ( requestItem )
177
+ }
178
+
179
+ console . log ( 'All requests have been sent!' )
180
+
181
+ const res = await Promise . all ( requestQueue )
182
+
183
+ const success : IRequestResItem [ ] = [ ]
184
+ const error : string [ ] = [ ]
185
+
186
+ // 通过类型分类
187
+ res . forEach ( ( item ) => {
188
+ if ( typeof item === 'string' ) {
189
+ return error . push ( item )
151
190
}
152
191
153
- batchRequestResHandle ( error , { id, ...requestRes } )
192
+ success . push ( item )
193
+ } )
194
+
195
+ error . forEach ( ( message ) => {
196
+ console . log ( message )
197
+ } )
198
+
199
+ return success
200
+ }
201
+
202
+ export async function syncBatchRequest (
203
+ requestConifgs : IRequestConfig [ ] ,
204
+ intervalTime : IIntervalTime | undefined
205
+ ) {
206
+ const isHaveIntervalTime = ! isUndefined ( intervalTime )
207
+ const isNumberIntervalTime = isNumber ( intervalTime )
208
+
209
+ console . log ( `Begin execution, mode: sync, total: ${ requestConifgs . length } ` )
154
210
155
- if ( isHaveIntervalTime && id !== total ) {
156
- const timeout = isNumberIntervalTime
157
- ? intervalTime
158
- : random ( intervalTime . max , intervalTime . min )
211
+ let id = 0
212
+ const requestRes : IRequestResItem [ ] = [ ]
213
+ for ( const requestConifg of requestConifgs ) {
214
+ id ++
159
215
160
- console . log ( `The ${ id } request is ${ state } , sleep for ${ timeout } ms` )
216
+ await useSleepByBatch (
217
+ isHaveIntervalTime ,
218
+ isNumberIntervalTime ,
219
+ intervalTime ,
220
+ id
221
+ )
161
222
162
- await sleep ( timeout )
163
- } else {
164
- console . log ( `The ${ id } request is ${ state } ` )
165
- console . log ( `All requests completed!` )
223
+ try {
224
+ const requestResItem = await request ( requestConifg )
225
+ requestRes . push ( { id, ...requestResItem } )
226
+ console . log ( `Request ${ id } is an success` )
227
+ } catch ( error : any ) {
228
+ console . log ( `Request ${ id } is an error: ${ error . message } ` )
166
229
}
167
230
}
231
+
232
+ console . log ( 'All requests are over!' )
233
+
234
+ return requestRes
168
235
}
0 commit comments