Skip to content

Commit a354df4

Browse files
committed
Docs: Add canonical async examples
1 parent c1a3536 commit a354df4

File tree

6 files changed

+121
-83
lines changed

6 files changed

+121
-83
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
title: 'debounce (async)'
3+
id: 'utils-debounce-async'
4+
exampleType: 'log'
5+
category: 'Utils'
6+
subcategory: 'Functions'
7+
description: 'Debounce asynchronous functions with promise sharing'
8+
tags: ['utils', 'functions', 'debounce', 'async', 'promise']
9+
selectedFile: 'index.js'
10+
tip: 'Multiple calls to a debounced async function share the same promise until it settles'
11+
---
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
title: 'throttle (async)'
3+
id: 'utils-throttle-async'
4+
exampleType: 'log'
5+
category: 'Utils'
6+
subcategory: 'Functions'
7+
description: 'Throttle asynchronous functions with proper promise handling'
8+
tags: ['utils', 'functions', 'throttle', 'async', 'promise']
9+
selectedFile: 'index.js'
10+
tip: 'Throttled async functions maintain leading/trailing behavior while handling promises correctly'
11+
---
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { debounce } from '@semantic-ui/utils';
2+
3+
async function saveData(data) {
4+
await new Promise(resolve => setTimeout(resolve, 50));
5+
console.log(`Saved: ${data}`);
6+
return `result: ${data}`;
7+
}
8+
9+
// async debouncing with promise sharing
10+
const debouncedSave = debounce(saveData, 200);
11+
12+
// all calls share the same promise
13+
Promise.all([
14+
debouncedSave('data1'),
15+
debouncedSave('data2'),
16+
debouncedSave('data3'), // only this executes
17+
]).then(results => {
18+
console.log('All results:', results); // all get same result
19+
});
20+
21+
// subsequent calls after settlement
22+
setTimeout(async () => {
23+
const result = await debouncedSave('data4');
24+
console.log('New result:', result);
25+
}, 400);
Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,30 @@
11
import { debounce } from '@semantic-ui/utils';
22

3-
// Sync function example
4-
function logMessage(message) {
5-
console.log(`Sync: ${message} at ${Date.now()}`);
6-
return `logged: ${message}`;
3+
function search(query) {
4+
console.log(`Searching: ${query}`);
5+
return query;
76
}
87

9-
// Async function example
10-
async function saveData(data) {
11-
await new Promise(resolve => setTimeout(resolve, 50));
12-
console.log(`Async: saved ${data}`);
13-
return `saved: ${data}`;
14-
}
15-
16-
// Basic debouncing - only last call executes
17-
console.log('Basic debouncing:');
18-
const debouncedLog = debounce(logMessage, 100);
19-
debouncedLog('call 1');
20-
debouncedLog('call 2');
21-
debouncedLog('call 3'); // Only this executes
22-
23-
// Async debouncing with promise sharing
24-
console.log('\nAsync debouncing (promise sharing):');
25-
const debouncedSave = debounce(saveData, 100);
26-
Promise.all([
27-
debouncedSave('data1'),
28-
debouncedSave('data2'),
29-
debouncedSave('data3') // Only this executes, all promises resolve to same result
30-
]).then(results => {
31-
console.log('All results:', results); // All get 'saved: data3'
32-
});
8+
// basic debouncing - only last call executes
9+
const debouncedSearch = debounce(search, 200);
10+
debouncedSearch('a');
11+
debouncedSearch('ab');
12+
debouncedSearch('abc'); // only this executes
3313

34-
// Leading execution
14+
// leading edge - executes on both leading and trailing edges
3515
setTimeout(() => {
36-
console.log('\nLeading execution:');
37-
const leadingDebounce = debounce(logMessage, 200, { leading: true });
38-
console.log('Immediate result:', leadingDebounce('immediate')); // Executes right away
39-
leadingDebounce('ignored'); // Debounced
16+
const leadingDebounce = debounce(search, 200, { leading: true });
17+
leadingDebounce('first'); // executes immediately (leading)
18+
leadingDebounce('second'); // executes after 200ms (trailing)
4019
}, 300);
4120

42-
// MaxWait option
21+
// maxWait - forces execution after maximum time
4322
setTimeout(() => {
44-
console.log('\nMaxWait forces execution:');
45-
const maxWaitDebounce = debounce(logMessage, 50, { maxWait: 120 });
46-
maxWaitDebounce('call1');
47-
setTimeout(() => maxWaitDebounce('call2'), 30);
48-
setTimeout(() => maxWaitDebounce('call3'), 60); // Forces execution at 120ms
49-
}, 600);
23+
const maxWaitDebounce = debounce(search, 300, { maxWait: 500 });
24+
maxWaitDebounce('input1');
25+
setTimeout(() => maxWaitDebounce('input2'), 100);
26+
setTimeout(() => maxWaitDebounce('input3'), 200);
27+
setTimeout(() => maxWaitDebounce('input4'), 400); // forces execution at 500ms
28+
// input5 triggers a new debounce cycle after maxWait
29+
setTimeout(() => maxWaitDebounce('input5'), 600); // executes 300ms later
30+
}, 600);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { throttle } from '@semantic-ui/utils';
2+
3+
async function fetchData(endpoint) {
4+
await new Promise(resolve => setTimeout(resolve, 50));
5+
console.log(`Fetched: ${endpoint}`);
6+
return `data from ${endpoint}`;
7+
}
8+
9+
// async throttling with promise handling
10+
const throttledFetch = throttle(fetchData, 200);
11+
12+
// first call executes immediately
13+
throttledFetch('/users').then(result => {
14+
console.log('First result:', result);
15+
});
16+
17+
// subsequent calls queue for trailing execution
18+
throttledFetch('/posts');
19+
throttledFetch('/comments'); // this replaces /posts
20+
21+
// rapid async calls
22+
setTimeout(() => {
23+
const rapidThrottle = throttle(fetchData, 150);
24+
for (let i = 1; i <= 4; i++) {
25+
rapidThrottle(`/api/v${i}`).then(result => {
26+
console.log(`API result: ${result}`);
27+
});
28+
}
29+
}, 400);
Lines changed: 24 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,36 @@
11
import { throttle } from '@semantic-ui/utils';
22

3-
// Sync function example
4-
function logEvent(event) {
5-
console.log(`Sync: ${event} at ${Date.now()}`);
6-
return `logged: ${event}`;
3+
function trackEvent(event) {
4+
console.log(`Event: ${event}`);
5+
return event;
76
}
87

9-
// Async function example
10-
async function apiCall(endpoint) {
11-
await new Promise(resolve => setTimeout(resolve, 50));
12-
console.log(`Async: called ${endpoint}`);
13-
return `response from ${endpoint}`;
14-
}
15-
16-
// Basic throttling - leading and trailing (default)
17-
console.log('Basic throttling (leading + trailing):');
18-
const throttledLog = throttle(logEvent, 200);
19-
console.log('First result:', throttledLog('event1')); // Executes immediately
20-
throttledLog('event2'); // Queued for trailing
21-
throttledLog('event3'); // Replaces event2
22-
23-
// Async throttling
24-
console.log('\nAsync throttling:');
25-
const throttledAPI = throttle(apiCall, 150);
26-
throttledAPI('/users').then(result => console.log('API result:', result));
27-
throttledAPI('/posts'); // Queued for trailing execution
8+
// basic throttling - leading and trailing (default)
9+
const throttled = throttle(trackEvent, 200);
10+
throttled('click1'); // executes immediately
11+
throttled('click2'); // queued for trailing
12+
throttled('click3'); // replaces click2
2813

29-
// Leading only - execute immediately, ignore subsequent
14+
// leading only
3015
setTimeout(() => {
31-
console.log('\nLeading only:');
32-
const leadingOnly = throttle(logEvent, 300, { leading: true, trailing: false });
33-
console.log('Immediate result:', leadingOnly('click1')); // Executes immediately
34-
leadingOnly('click2'); // Ignored
35-
leadingOnly('click3'); // Ignored
16+
const leadingOnly = throttle(trackEvent, 200, { leading: true, trailing: false });
17+
leadingOnly('scroll1'); // executes immediately
18+
leadingOnly('scroll2'); // ignored
19+
leadingOnly('scroll3'); // ignored
3620
}, 400);
3721

38-
// Trailing only - execute once after throttle period
22+
// trailing only
3923
setTimeout(() => {
40-
console.log('\nTrailing only:');
41-
const trailingOnly = throttle(logEvent, 200, { leading: false, trailing: true });
42-
trailingOnly('scroll1'); // Queued
43-
trailingOnly('scroll2'); // Replaces scroll1
44-
trailingOnly('scroll3'); // This executes after 200ms
45-
}, 800);
24+
const trailingOnly = throttle(trackEvent, 200, { leading: false, trailing: true });
25+
trailingOnly('move1'); // queued
26+
trailingOnly('move2'); // replaces move1
27+
trailingOnly('move3'); // executes after 200ms
28+
}, 700);
4629

47-
// High frequency events simulation
30+
// high frequency events
4831
setTimeout(() => {
49-
console.log('\nHigh frequency events:');
50-
const throttledScroll = throttle(logEvent, 100);
51-
for (let i = 1; i <= 8; i++) {
52-
setTimeout(() => throttledScroll(`scroll${i}`), i * 20);
32+
const throttledInput = throttle(trackEvent, 100);
33+
for (let i = 1; i <= 5; i++) {
34+
setTimeout(() => throttledInput(`input${i}`), i * 30);
5335
}
54-
// Only first and last will execute due to throttling
55-
}, 1200);
36+
}, 1000);

0 commit comments

Comments
 (0)