
Advanced Asynchronous JavaScript Patterns
A deep dive into JavaScript async flow control, breaking down debounce and throttle mechanics, concurrent Promise resolution, and EventEmitter patterns for scalable, leak-free applications.
Managing Asynchronous Flow in JavaScript
Modern Web Development နဲ့ Backend Engineering တွေမှာ JavaScript ရဲ့ Single-threaded Event Loop သဘောတရားကို နားလည်ထားဖို့ အရေးကြီးပါတယ်။ Browser UI ပေါ်မှာ User ရဲ့ Rapid Input Events တွေကို ကိုင်တွယ်တာပဲဖြစ်ဖြစ်၊ Node.js Backend မှာ Multiple Microservices ဆီက Data တွေကို ပြိုင်တူ ဆွဲယူတာပဲဖြစ်ဖြစ်၊ Asynchronous Operations တွေကို စနစ်တကျ ထိန်းချုပ်နိုင်စွမ်း မရှိရင် Performance ကျဆင်းခြင်းနဲ့ Memory Leaks တွေ ဖြစ်ပေါ်လာနိုင်ပါတယ်။
အထူးသဖြင့် Event Frequency ကို ကန့်သတ်ပေးတဲ့ Debounce နဲ့ Throttle၊ Concurrency ကို စီမံပေးတဲ့ Promise.all မိသားစု၊ ပြီးတော့ Decoupled Event-driven Architecture တွေမှာ သုံးတဲ့ EventEmitter တို့ဟာ Developer တိုင်း နေ့စဉ် ရင်ဆိုင်နေရတဲ့ အဓိက Patterns တွေ ဖြစ်ပါတယ်။
ဒီ Patterns တစ်ခုချင်းစီမှာ မတူညီတဲ့ အားသာချက်တွေ၊ လုပ်ဆောင်ချက် ကန့်သတ်ချက်တွေနဲ့ သတိထားရမယ့် Pitfalls တွေ ရှိကြပါတယ်။ အဆိုပါ အင်ဂျင်နီယာဆိုင်ရာ အခြေခံ သဘောတရားတွေကို အသေးစိတ် လေ့လာကြည့်ကြပါမယ်။
Debounce vs Throttle: Controlling Event Frequency
DOM Events တွေဖြစ်တဲ့ scroll, resize, mousemove ဒါမှမဟုတ် Search Input ရဲ့ keyup စတာတွေဟာ စက္ကန့်ပိုင်းအတွင်း အကြိမ်ပေါင်း ဒါဇင်ချီ ထွက်ပေါ်လေ့ ရှိပါတယ်။ အဲဒီ Event တိုင်းမှာ API Request လှမ်းပို့တာ သို့မဟုတ် DOM Layout ကို ပြန်တွက်ချက်တာမျိုး လုပ်ဆောင်ရင် Browser UI ဟာ ချက်ချင်း Freezing ဖြစ်သွားနိုင်ပါတယ်။
ဒီပြဿနာကို ဖြေရှင်းဖို့ Rate-limiting နည်းလမ်း နှစ်မျိုး ရှိပါတယ်။
-
Debounce (စောင့်ဆိုင်းပြီးမှ ခေါ်ဆိုခြင်း): User က Action တစ်ခုခုကို ဆက်တိုက် လုပ်ဆောင်နေချိန်မှာ Callback ကို ချက်ချင်း မခေါ်သေးဘဲ ခေတ္တ ဆိုင်းငံ့ထားပါတယ်။ သတ်မှတ်ထားတဲ့ Delay (ဥပမာ 300ms) အတွင်း နောက်ထပ် Event အသစ် ထပ်မံ မရောက်ရှိတော့တဲ့အခါ (User ရပ်နားသွားတဲ့အခါ) မှသာ နောက်ဆုံး Event ကို တစ်ကြိမ်တည်း Run ပေးပါတယ်။ Search Bar Auto-complete နဲ့ Form Auto-save တွေအတွက် အကောင်းဆုံး ဖြစ်ပါတယ်။
-
Throttle (ပုံမှန် အပိုင်းအခြားဖြင့် ခေါ်ဆိုခြင်း): Events တွေ ဘယ်လောက်ပဲ အကြိမ်ရေများများ ဆက်တိုက် ဝင်ရောက်နေပါစေ၊ သတ်မှတ်ထားတဲ့ Time Window (ဥပမာ 200ms) အတွင်းမှာ အများဆုံး တစ်ကြိမ်သာ မပျက်မကွက် Execute လုပ်ပေးပါတယ်။ Infinite Scroll Trigger လုပ်ခြင်း၊ Window Resize အတိုင်း Canvas Redraw လုပ်ခြင်းနဲ့ Video Player Progress Tracking တွေအတွက် အသင့်တော်ဆုံး ဖြစ်ပါတယ်။
Implementing Type-Safe Debounce and Throttle
Third-party Libraries (Lodash) ကို မသုံးဘဲ Vanilla TypeScript နဲ့ ရိုးရှင်းပြီး Memory Clean ဖြစ်တဲ့ Implementation တွေကို အောက်ပါအတိုင်း တည်ဆောက်နိုင်ပါတယ်။
export function debounce<T extends (...args: any[]) => void>(fn: T, delay: number) {
let timer: ReturnType<typeof setTimeout> | null = null
return (...args: Parameters<T>) => {
if (timer) clearTimeout(timer)
timer = setTimeout(() => fn(...args), delay)
}
}
export function throttle<T extends (...args: any[]) => void>(fn: T, limit: number) {
let lastCall = 0
return (...args: Parameters<T>) => {
const now = Date.now()
if (now - lastCall >= limit) {
lastCall = now
fn(...args)
}
}
}
ဒီ Functions တွေဟာ Closure ကို အသုံးပြုပြီး timer ID နဲ့ lastCall Timestamp ကို အတွင်းပိုင်းမှာ သိမ်းဆည်းထားပါတယ်။ React Components တွေထဲမှာ သုံးစွဲတဲ့အခါ Re-render တိုင်းမှာ Timer အသစ် ပြန်မတည်ဆောက်မိစေဖို့ useCallback သို့မဟုတ် Custom Hook (useDebounce) တွေနဲ့ တွဲဖက် အသုံးပြုသင့်ပါတယ်။
Concurrent Asynchronous Workflows with Promise.all
Backend API အများစုမှာ User Profile, Permissions နဲ့ Notifications Data တွေကို တစ်ပြိုင်နက်တည်း ရယူဖို့ လိုအပ်လေ့ ရှိပါတယ်။ await ကို တစ်ကြောင်းချင်းစီ ဆင့်ခေါ်နေရင် Sequential Execution ဖြစ်သွားပြီး Response Time ပေါင်းစည်း ကြာမြင့်သွားပါမယ်။
ဒီနေရာမှာ Promise.all ကို အသုံးပြုပြီး Parallel Execution ပြုလုပ်နိုင်ပါတယ်။
- Promise.all (Fail-Fast Concurrency):
Promises အားလုံးကို တစ်ပြိုင်နက် စတင်စေပြီး အားလုံး Resolved ဖြစ်မှသာ Array အနေနဲ့ ပြန်ပေးပါတယ်။ ဒါပေမဲ့ Promise တစ်ခုခုက
rejectဖြစ်သွားတာနဲ့ ကျန်တဲ့ Promises တွေ ပြီးဆုံးသည်အထိ မစောင့်တော့ဘဲ ချက်ချင်း Error ပြန်ထုတ်ပေးပါတယ်။ All-or-Nothing လိုအပ်တဲ့ Transactional Operations တွေအတွက် သင့်တော်ပါတယ်။ - Promise.allSettled (Resilient Aggregation):
Promises တချို့ Error တက်သွားရင်တောင်မှ ကျန်တဲ့ အောင်မြင်တဲ့ ရလဒ်တွေကို စုစည်း ရယူချင်တဲ့အခါ သုံးပါတယ်။ Promise တစ်ခုချင်းစီရဲ့
{ status: 'fulfilled', value }သို့မဟုတ်{ status: 'rejected', reason }အခြေအနေကို စစ်ဆေးနိုင်ပါတယ်။ Batch Email Dispatch သို့မဟုတ် Dashboard Widgets Loading တွေအတွက် အလွန် အသုံးဝင်ပါတယ်။
သတိပြုရမယ့် အချက်တစ်ခုကတော့ Promise.all ထဲကို Database Queries ထောင်ပေါင်းများစွာ တစ်ပြိုင်နက် ထည့်မပစ်သင့်ပါဘူး။ Database Connection Pool ကုန်ခမ်းသွားနိုင်တဲ့အတွက် p-limit လိုမျိုး Concurrency Limiter တွေနဲ့ အုပ်စုဖွဲ့ပြီး (Batching) ခေါ်ဆိုသင့်ပါတယ်။
Event-Driven Architecture with EventEmitter
Node.js Runtime ရဲ့ အခြေခံ အုတ်မြစ်ဖြစ်တဲ့ EventEmitter ဟာ Producer နဲ့ Consumer ကြားက တင်းကျပ်တဲ့ ချိတ်ဆက်မှု (Tight Coupling) ကို ဖြည်ထုတ်ပေးတဲ့ Observer Pattern ဖြစ်ပါတယ်။
ဥပမာ User တစ်ယောက် Register လုပ်ပြီးချိန်မှာ Welcome Email ပို့တာ၊ Analytics Event မှတ်တမ်းတင်တာနဲ့ Audit Log ရေးတာတွေကို Registration Service ထဲမှာ တိုက်ရိုက် မရေးဘဲ Event Emit လုပ်ပြီး ခွဲထုတ်နိုင်ပါတယ်။
import { EventEmitter } from 'node:events'
interface UserEvents {
registered: (user: { id: string; email: string }) => Promise<void>
}
class UserEventEmitter extends EventEmitter {}
const userEvents = new UserEventEmitter()
userEvents.on('registered', async (user) => {
// Asynchronous background task execution
await sendWelcomeEmail(user.email)
})
ဒီနေရာမှာ သတိပြုရမယ့် အချက်တစ်ခုက EventEmitter.emit() ဟာ Synchronous Function ဖြစ်ပါတယ်။ Listener က async Function ဖြစ်နေရင်တောင် emit() ခေါ်ဆိုမှုက အဲဒီ Promise ပြီးဆုံးအောင် စောင့်မနေပါဘူး။ ဒါကြောင့် Listener ထဲမှာ Unhandled Rejection မဖြစ်စေဖို့အတွက် try/catch နဲ့ သေချာ ထိန်းချုပ်ထားရပါမယ်။
Memory Leaks and Cleanup Lifecycle
Asynchronous Programming မှာ အဖြစ်အများဆုံး ချွတ်ယွင်းချက်ကတော့ သက်တမ်းကုန်သွားတဲ့ Listeners တွေနဲ့ Timers တွေကို ပြန်လည် မရှင်းလင်းဘဲ ထားခဲ့မိတာ ဖြစ်ပါတယ်။
Node.js မှာ EventEmitter တစ်ခုထဲကို Listeners ပေါင်း ၁၀ ခုထက်ပိုပြီး Register လုပ်မိတဲ့အခါ MaxListenersExceededWarning ပေါ်လာတတ်ပါတယ်။ ဒါဟာ အများအားဖြင့် Request Lifecycle တိုင်းမှာ Event Listener အသစ်တွေကို အထပ်ထပ် ထည့်သွင်းမိပြီး ပြန်မဖျက်တဲ့အခါ ဖြစ်ပွားလေ့ ရှိပါတယ်။
- EventEmitter Cleanup:
အလုပ်ပြီးမြောက်သွားတဲ့ Listener တွေကို
emitter.removeListener()သို့မဟုတ်emitter.off()နဲ့ ပြန်လည် ဖယ်ရှားရပါမယ်။ တစ်ကြိမ်တည်းသာ နားထောင်မယ့် Event အတွက်ဆိုရင်emitter.once()ကို သုံးနိုင်သလို၊ Modern Node.js မှာAbortControllerရဲ့ Signal ကို ပေးပို့ပြီး အလိုအလျောက် Unsubscribe လုပ်စေနိုင်ပါတယ်။ - UI Component Cleanup:
React ရဲ့
useEffectထဲမှာ Debounce/Throttle Timers တွေကို သုံးတဲ့အခါ Component Unmount ဖြစ်ချိန်မှာclearTimeoutသို့မဟုတ်cancel()Method ကို Cleanup Function ထဲကနေ မဖြစ်မနေ ပြန်လည် ခေါ်ဆိုပေးရပါမယ်။ မဟုတ်ပါက Unmounted Component ပေါ်မှာ State Update လုပ်မိပြီး Memory Leak ဖြစ်ပေါ်စေနိုင်ပါတယ်။
Comparing Asynchronous Control Strategies
စနစ်တစ်ခုရဲ့ လိုအပ်ချက်ပေါ် မူတည်ပြီး ရွေးချယ်နိုင်တဲ့ အဓိက Asynchronous Flow Patterns တွေကို အောက်ပါအတိုင်း နှိုင်းယှဉ် သုံးသပ်နိုင်ပါတယ်။
| Pattern | Primary Mechanism | Concurrency Model | Failure Handling | Best Use Case |
|---|---|---|---|---|
| Debounce | Delay execution until idle period expires | Serial delayed call | Trapped in callback | Search bar auto-complete, window resize |
| Throttle | Limit execution rate to fixed time intervals | Periodic execution | Trapped in callback | Infinite scroll, scroll-spy, game loop inputs |
| Promise.all | Execute promises concurrently in parallel | Parallel aggregation | Fail-fast on first error | Independent I/O fetches, parallel queries |
| Promise.allSettled | Wait for all promises regardless of outcome | Parallel non-blocking | Collects all results/errors | Batch email dispatch, multi-service metrics |
| EventEmitter | Decoupled pub-sub event dispatching | Event-driven async | Emits dedicated error event | Background task notifications, stream pipelines |
Final Thoughts
Asynchronous Programming ဆိုတာ Code ကို async/await နဲ့ ရေးသားရုံနဲ့ မပြီးပြည့်စုံပါဘူး။ Event တွေရဲ့ ဖြစ်ပေါ်မှု အကြိမ်ရေကို ဘယ်လို ထိန်းကျောင်းမလဲ၊ ပြိုင်တူ စီးဆင်းနေတဲ့ I/O လုပ်ငန်းတွေကို ဘယ်လို ကန့်သတ်မလဲ၊ ပြီးတော့ စနစ်ရဲ့ Lifecycle ပြီးဆုံးချိန်မှာ Resources တွေကို ဘယ်လို သန့်ရှင်းရေး ပြုလုပ်မလဲဆိုတဲ့ အချက်တွေကို ထည့်သွင်း စဉ်းစားရပါမယ်။
Debounce နဲ့ Throttle ဖြင့် UI jank နဲ့ network traffic burst တွေကို ထိန်းညှိခြင်း၊ Promise.all နဲ့ allSettled ကို failure tolerance ပေါ်မူတည်ပြီး သင့်တင့်စွာ ခွဲခြား သုံးစွဲခြင်း၊ ပြီးတော့ EventEmitter listeners တွေကို cleanup လုပ်ပေးခြင်းက memory leaks မဖြစ်စေဘဲ responsive ဖြစ်တဲ့ high-performance JavaScript applications တွေကို စိတ်ချလက်ချ ထိန်းကျောင်းနိုင်စေပါတယ်။