@@ -153,11 +153,12 @@ test('onRateLimit() with microtask race condition', async () => {
153153
154154 // Try to attach listener after tasks are queued
155155 await Promise . resolve ( ) ; // Microtask delay
156- queue . onRateLimit ( ( ) => {
156+ const rateLimitPromise = ( async ( ) => {
157+ await queue . onRateLimit ( ) ;
157158 rateLimitCalled = true ;
158- } ) ;
159+ } ) ( ) ;
159160
160- await Promise . all ( promises ) ;
161+ await Promise . all ( [ ... promises , rateLimitPromise ] ) ;
161162 assert . ok ( rateLimitCalled , 'onRateLimit should handle late attachment' ) ;
162163} ) ;
163164
@@ -173,18 +174,16 @@ test('onRateLimitCleared() with microtask race condition', async () => {
173174
174175 await delay ( 20 ) ; // Let rate limit trigger
175176
176- let clearedCalled = false ;
177-
178- // Attach listener during rate limit
179- queue . onRateLimitCleared ( ( ) => {
180- clearedCalled = true ;
181- } ) ;
177+ // Wait for clear using the promise API
178+ const clearedPromise = queue . onRateLimitCleared ( ) ;
182179
183180 // Wait for clear
184181 await queue . onIdle ( ) ;
185182 await delay ( 110 ) ; // Past interval
186183
187- assert . ok ( clearedCalled , 'onRateLimitCleared should handle attachment during rate limit' ) ;
184+ // The promise should resolve when rate limit is cleared
185+ await clearedPromise ;
186+ assert . ok ( true , 'onRateLimitCleared should handle attachment during rate limit' ) ;
188187} ) ;
189188
190189test ( 'onRateLimit() called during state transition' , async ( ) => {
@@ -201,14 +200,16 @@ test('onRateLimit() called during state transition', async () => {
201200 sequence . push ( 'task1-end' ) ;
202201 } ) ;
203202
204- queue . add ( async ( ) => {
203+ const secondTask = queue . add ( async ( ) => {
205204 sequence . push ( 'task2' ) ;
206205 } ) ;
207206
208- // Attach listener during execution
209- await queue . onRateLimit ( ( ) => {
210- sequence . push ( 'rate-limit' ) ;
211- } ) ;
207+ // Wait for rate limit to be triggered
208+ await queue . onRateLimit ( ) ;
209+ sequence . push ( 'rate-limit' ) ;
210+
211+ // Wait for second task to complete
212+ await secondTask ;
212213
213214 await queue . onIdle ( ) ;
214215
@@ -248,13 +249,13 @@ test('onRateLimit/onRateLimitCleared rapid transitions', async () => {
248249 assert . ok ( events . includes ( 'cleared' ) ) ;
249250} ) ;
250251
251- test ( 'onRateLimit() never resolves if queue is cleared ' , async ( ) => {
252+ test ( 'onRateLimit() resolves when rate limit is triggered ' , async ( ) => {
252253 const queue = new PQueue ( {
253254 interval : 100 ,
254255 intervalCap : 1 ,
255256 } ) ;
256257
257- queue . add ( async ( ) => delay ( 10 ) ) ;
258+ // Add tasks to eventually trigger rate limit
258259 queue . add ( async ( ) => delay ( 10 ) ) ;
259260
260261 let rateLimitResolved = false ;
@@ -263,15 +264,14 @@ test('onRateLimit() never resolves if queue is cleared', async () => {
263264 rateLimitResolved = true ;
264265 } ) ( ) ;
265266
266- // Clear queue before rate limit can be established
267- await delay ( 5 ) ;
268- queue . clear ( ) ;
267+ // Add another task which will trigger rate limit
268+ queue . add ( async ( ) => delay ( 10 ) ) ;
269269
270- // Give time for any pending operations
270+ // Give time for rate limit to be triggered
271271 await delay ( 20 ) ;
272272
273- // OnRateLimit should not resolve since queue was cleared
274- assert . ok ( ! rateLimitResolved , 'onRateLimit should not resolve after clear ' ) ;
273+ // OnRateLimit should have resolved since we hit rate limit
274+ assert . ok ( rateLimitResolved , 'onRateLimit should resolve when rate limit is triggered ' ) ;
275275
276276 // Clean up
277277 await queue . onIdle ( ) ;
0 commit comments