@@ -14,7 +14,8 @@ const defaultConfig = {
1414 retryOnHttpServerError : true ,
1515 maxNetworkRetries : 3 ,
1616 networkRetryDelay : 100 , // Base delay for network retries (ms)
17- networkBackoffStrategy : 'exponential' // 'exponential' or 'fixed'
17+ networkBackoffStrategy : 'exponential' , // 'exponential' or 'fixed'
18+ delayMs : null // Delay in milliseconds before making each request
1819}
1920
2021export function ConcurrencyQueue ( { axios, config } ) {
@@ -175,16 +176,22 @@ export function ConcurrencyQueue ({ axios, config }) {
175176
176177 // Initial shift will check running request,
177178 // and adds request to running queue if max requests are not running
178- this . initialShift = ( ) => {
179+ this . initialShift = async ( ) => {
179180 if ( this . running . length < this . config . maxRequests && ! this . paused ) {
180- shift ( )
181+ await shift ( )
181182 }
182183 }
183184
184185 // INTERNAL: Shift the queued item to running queue
185- const shift = ( ) => {
186+ const shift = async ( ) => {
186187 if ( this . queue . length && ! this . paused ) {
187188 const queueItem = this . queue . shift ( )
189+
190+ // Add configurable delay before making the request if specified
191+ if ( this . config . delayMs && this . config . delayMs > 0 ) {
192+ await new Promise ( resolve => setTimeout ( resolve , this . config . delayMs ) )
193+ }
194+
188195 queueItem . resolve ( queueItem . request )
189196 this . running . push ( queueItem )
190197 }
@@ -197,7 +204,7 @@ export function ConcurrencyQueue ({ axios, config }) {
197204
198205 this . push = requestPromise => {
199206 this . queue . push ( requestPromise )
200- this . initialShift ( )
207+ this . initialShift ( ) . catch ( console . error )
201208 }
202209
203210 this . clear = ( ) => {
@@ -312,7 +319,7 @@ export function ConcurrencyQueue ({ axios, config }) {
312319 return refreshToken ( )
313320 } else {
314321 for ( let i = 0 ; i < this . config . maxRequests ; i ++ ) {
315- this . initialShift ( )
322+ this . initialShift ( ) . catch ( console . error )
316323 }
317324 }
318325 } , time ) )
@@ -360,7 +367,7 @@ export function ConcurrencyQueue ({ axios, config }) {
360367 }
361368 } )
362369 for ( let i = 0 ; i < this . config . maxRequests ; i ++ ) {
363- this . initialShift ( )
370+ this . initialShift ( ) . catch ( console . error )
364371 }
365372 } )
366373 }
0 commit comments