Skip to content

Commit cba4bf0

Browse files
committed
Formatting
1 parent 18f0132 commit cba4bf0

4 files changed

Lines changed: 87 additions & 87 deletions

File tree

Fetch API/abortFetch.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@ const {signal} = controller;
44
// console.log(signal);
55

66
const fetch1 = (req, abortSig) => {
7-
const controller = new AbortController();
8-
const {signal} = controller;
9-
if (abortSig === 'abort') controller.abort();
10-
// controller.abort('manual abort');
11-
// controller.abort();
7+
const controller = new AbortController();
8+
const {signal} = controller;
9+
if (abortSig === 'abort') controller.abort();
10+
// controller.abort('manual abort');
11+
// controller.abort();
1212

13-
return fetch(req, {signal})
14-
.then(res => res.json())
15-
.then(data => console.log('fetch1', data.id))
16-
.catch(e => console.error('fetch1 Error: ', e));
13+
return fetch(req, {signal})
14+
.then(res => res.json())
15+
.then(data => console.log('fetch1', data.id))
16+
.catch(e => console.error('fetch1 Error: ', e));
1717
};
1818

1919
const fetch2 = req => {
20-
return fetch(req, {signal})
21-
.then(res => res.json())
22-
.then(data => console.log('fetch2', data.id))
23-
.catch(e => console.error('fetch2 Error: ', e));
20+
return fetch(req, {signal})
21+
.then(res => res.json())
22+
.then(data => console.log('fetch2', data.id))
23+
.catch(e => console.error('fetch2 Error: ', e));
2424
};
2525

2626
// controller.abort();

Fetch API/fetchQueue.js

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,43 @@ const fetchQueue = [];
22
let isFetching = false;
33

44
function 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

1414
function 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

3939
function handleQueue(request) {
40-
fetchQueue.push(request);
41-
processQueue();
40+
fetchQueue.push(request);
41+
processQueue();
4242

4343
}
4444
handleQueue({ 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

Fetch API/fetchWithDelay.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@
33

44
const t0 = performance.now();
55
function fetchWithDelay(url, delay = 1000) { // delay must be at least a second or setTimeout will resolve before fetch is done
6-
return new Promise((resolve, reject) => {
7-
fetch(url)
8-
.then(response => resolve(response))
9-
.catch(error => reject(error));
10-
setTimeout(() => {
11-
const t1 = performance.now();
12-
console.log(`Call took ${t1 - t0} milliseconds.`);
13-
resolve(null); // Resolve after the delay even if fetch fails
14-
}, delay);
15-
});
6+
return new Promise((resolve, reject) => {
7+
fetch(url)
8+
.then(response => resolve(response))
9+
.catch(error => reject(error));
10+
setTimeout(() => {
11+
const t1 = performance.now();
12+
console.log(`Call took ${t1 - t0} milliseconds.`);
13+
resolve(null); // Resolve after the delay even if fetch fails
14+
}, delay);
15+
});
1616
}
1717

1818
const t1 = performance.now();
1919
fetchWithDelay('http://192.168.0.5:8000/r/MapPorn', 1000)
20-
// then(data => data.json()
21-
.then(data => data.json())
22-
.then(json => console.log(json.id))
23-
.catch(e => console.error(e.message))
20+
// then(data => data.json()
21+
.then(data => data.json())
22+
.then(json => console.log(json.id))
23+
.catch(e => console.error(e.message))
2424
// fetchWithDelay('https://www.reddit.com/user/lolifax/about.json');
2525

2626
// { url: 'https://www.reddit.com/user/Deep-Neighborhood587/about.json', delay: 1000 },
@@ -29,4 +29,4 @@ fetchWithDelay('http://192.168.0.5:8000/r/MapPorn', 1000)
2929
// { url: 'https://www.reddit.com/user/Anadyne/about.json', delay: 1000 },
3030
// { url: 'https://www.reddit.com/user/rememberall/about.json', delay: 1000 },
3131
// { url: 'https://www.reddit.com/user/SM-68/about.json', delay: 1000 }
32-
// { url: 'https://www.reddit.com/user/[deleted]/about.json', delay: 15000 }
32+
// { url: 'https://www.reddit.com/user/[deleted]/about.json', delay: 15000 }

Fetch API/fetchWithDelayAsyncModified.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,31 @@ let isFetching = false;
1818
const results = [];
1919

2020
const getData = async item => {
21-
if(item) q.push(item);
22-
if(q.length && !isFetching){
23-
isFetching = true;
24-
await delay();
25-
const res = await fetch(q.shift());
26-
const json = await res.json();
27-
isFetching = false;
28-
results.push(json.id);
29-
getData();
30-
} else if (isFetching) {
31-
console.log('fetch blocked for', item );
32-
}
21+
if(item) q.push(item);
22+
if(q.length && !isFetching){
23+
isFetching = true;
24+
await delay();
25+
const res = await fetch(q.shift());
26+
const json = await res.json();
27+
isFetching = false;
28+
results.push(json.id);
29+
getData();
30+
} else if (isFetching) {
31+
console.log('fetch blocked for', item );
32+
}
3333
}
3434

3535
// const items = ['http://192.168.0.5:8000/user/405_notallowed','http://192.168.0.5:8000/user/Adventurous_Ant7239'];
3636
// getDataSeries(items).then((r)=>console.log(r));
3737
const data = async () => {
38-
const id = await getData('http://192.168.0.5:8000/user/405_notallowed');
39-
console.log('data =>' ,id, results);
38+
const id = await getData('http://192.168.0.5:8000/user/405_notallowed');
39+
console.log('data =>' ,id, results);
4040
};
4141
data();
4242

4343
const data2 = async () => {
44-
const id = await getData('http://192.168.0.5:8000/user/Adventurous_Ant7239');
45-
console.log('data2 =>', id, results);
44+
const id = await getData('http://192.168.0.5:8000/user/Adventurous_Ant7239');
45+
console.log('data2 =>', id, results);
4646
};
4747
data2();
48-
console.log(results);
48+
console.log(results);

0 commit comments

Comments
 (0)