@@ -2,43 +2,43 @@ const fetchQueue = [];
22let isFetching = false ;
33
44function fetchWithDelay ( url , delay ) {
5- return new Promise ( ( resolve , reject ) => {
6- setTimeout ( ( ) => {
7- fetch ( url )
8- . then ( response => resolve ( response ) )
9- . catch ( error => reject ( error ) ) ;
10- } , delay ) ;
11- } ) ;
5+ return new Promise ( ( resolve , reject ) => {
6+ setTimeout ( ( ) => {
7+ fetch ( url )
8+ . then ( response => resolve ( response ) )
9+ . catch ( error => reject ( error ) ) ;
10+ } , delay ) ;
11+ } ) ;
1212}
1313
1414function processQueue ( ) {
15- if ( ! isFetching && fetchQueue . length > 0 ) {
16- isFetching = true ;
17- const { url, delay } = fetchQueue . shift ( ) ;
18- fetchWithDelay ( url , delay )
19- . then ( response => {
20- if ( response ) {
21- // Process the response
22- const json = response . json ( ) ;
23- json . then ( r => console . log ( 'r.id' , r . id ) ) ;
24- } else {
25- // Handle timeout
26- }
27- isFetching = false ;
28- processQueue ( ) ; // Continue processing the queue
29- } )
30- . catch ( error => {
31- console . log ( 'Error: ' , error . message ) ;
32- // Handle fetch error
33- isFetching = false ;
34- processQueue ( ) ; // Continue processing the queue
35- } ) ;
36- }
15+ if ( ! isFetching && fetchQueue . length > 0 ) {
16+ isFetching = true ;
17+ const { url, delay } = fetchQueue . shift ( ) ;
18+ fetchWithDelay ( url , delay )
19+ . then ( response => {
20+ if ( response ) {
21+ // Process the response
22+ const json = response . json ( ) ;
23+ json . then ( r => console . log ( 'r.id' , r . id ) ) ;
24+ } else {
25+ // Handle timeout
26+ }
27+ isFetching = false ;
28+ processQueue ( ) ; // Continue processing the queue
29+ } )
30+ . catch ( error => {
31+ console . log ( 'Error: ' , error . message ) ;
32+ // Handle fetch error
33+ isFetching = false ;
34+ processQueue ( ) ; // Continue processing the queue
35+ } ) ;
36+ }
3737}
3838
3939function handleQueue ( request ) {
40- fetchQueue . push ( request ) ;
41- processQueue ( ) ;
40+ fetchQueue . push ( request ) ;
41+ processQueue ( ) ;
4242
4343}
4444handleQueue ( { url : 'http://192.168.0.5:8000/user/405_notallowed' , delay : 2000 } ) ;
@@ -47,17 +47,17 @@ handleQueue({ url: 'http://192.168.0.5:8000/r/MapPorn', delay: 2000 });
4747// handleQueue({ url: 'https://www.reddit.com/user/lolifax/about.json', delay: 2000 });
4848// Usage example
4949// fetchQueue.push(
50- // { url: 'http://192.168.0.5:8000/user/405_notallowed', delay: 2000 },
51- // { url: 'http://192.168.0.5:8000/user/Adventurous_Ant7239', delay: 3000 },
52- // { url: 'http://192.168.0.5:8000/r/MapPorn', delay: 2000 },
53- // { url: 'https://www.reddit.com/user/lolifax/about.json', delay: 2000 },
54- // { url: 'https://www.reddit.com/user/Deep-Neighborhood587/about.json', delay: 1000 },
55- // { url: 'https://www.reddit.com/user/RatsWithLongTails/about.json', delay: 1000 },
56- // { url: 'https://www.reddit.com/user/PervyFather1973/about.json', delay: 1000 },
57- // { url: 'https://www.reddit.com/user/Anadyne/about.json', delay: 1000 },
58- // { url: 'https://www.reddit.com/user/rememberall/about.json', delay: 1000 },
59- // { url: 'https://www.reddit.com/user/SM-68/about.json', delay: 1000 }
60- // { url: 'https://www.reddit.com/user/[deleted]/about.json', delay: 15000 }
50+ // { url: 'http://192.168.0.5:8000/user/405_notallowed', delay: 2000 },
51+ // { url: 'http://192.168.0.5:8000/user/Adventurous_Ant7239', delay: 3000 },
52+ // { url: 'http://192.168.0.5:8000/r/MapPorn', delay: 2000 },
53+ // { url: 'https://www.reddit.com/user/lolifax/about.json', delay: 2000 },
54+ // { url: 'https://www.reddit.com/user/Deep-Neighborhood587/about.json', delay: 1000 },
55+ // { url: 'https://www.reddit.com/user/RatsWithLongTails/about.json', delay: 1000 },
56+ // { url: 'https://www.reddit.com/user/PervyFather1973/about.json', delay: 1000 },
57+ // { url: 'https://www.reddit.com/user/Anadyne/about.json', delay: 1000 },
58+ // { url: 'https://www.reddit.com/user/rememberall/about.json', delay: 1000 },
59+ // { url: 'https://www.reddit.com/user/SM-68/about.json', delay: 1000 }
60+ // { url: 'https://www.reddit.com/user/[deleted]/about.json', delay: 15000 }
6161// );
6262// fetchQueue.push({ url: 'http://192.168.0.5:8000/r/MapPorn', delay: 5000 }); // Add fetch request to the queue
6363// processQueue(); // Start processing the queue
0 commit comments