Thuta Learning
JavaScript
AdvancedProgrammingအခြေခံ

Async JS Intro

JavaScriptသင်ခန်းစာ 3325 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Synchronous code က call stack ပေါ်မှာ တစ်ခုပြီးမှ တစ်ခု run ကြောင်းနဲ့ blocking ဆိုတာ ဘာလဲ ရှင်းပြနိုင်ဖို့
  • setTimeout(fn, 0) က ချက်ချင်း မဟုတ်ဘဲ stack ရှင်းမှ run ကြောင်း ခန့်မှန်းနိုင်ဖို့
  • Promise reaction (microtask) က timer callback (task) ထက် အရင် run ကြောင်း သိဖို့
  • CPU အများကြီး သုံးတဲ့ loop က event loop ကို ပိတ်ဆို့ကြောင်း သက်သေပြနိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းရရင်

စားဖိုမှူး တစ်ယောက်တည်းရှိတဲ့ မီးဖိုချောင်ကို စဉ်းစားကြည့်ပါ။ ထမင်းအိုးကို ဖွင့်ပြီး ပြီးတဲ့အထိ ရပ်စောင့်နေရင် တခြား ဟင်းတွေ မချက်နိုင်ပါဘူး။ ဒါကြောင့် ထမင်းပေါင်းအိုးကို timer ပေးထားခဲ့ပြီး ဟင်းဆက်ချက်ပါတယ်၊ timer မြည်တော့မှ ထမင်းဆီ ပြန်လာပါတယ်။ JavaScript ကလည်း အဲဒီလိုပါပဲ — thread တစ်ခုတည်းပဲ ရှိပေမယ့် ကြာမယ့် အလုပ်တွေကို runtime ဆီ လွှဲထားပြီး ရှေ့ဆက်ပါတယ်။

Timer မြည်တာ၊ server က data ပြန်ပို့တာ၊ user က click လုပ်တာ စတဲ့ "ပြီးပြီ" ဆိုတဲ့ အချက်ပြတွေက တန်းစီပြီး စောင့်ပါတယ်။ စားဖိုမှူး လက်အားတာနဲ့ တန်းစီထားတဲ့ အလုပ်တွေကို တစ်ခုချင်း ယူလုပ်ပါတယ်။ အဲဒီလို လှည့်ပတ် စစ်ပေးနေတာကို event loop လို့ ခေါ်ပါတယ်။

Problem → Why → Solution

ပြဿနာ: 0 ms timer ဆိုပေမယ့် data မရောက်သေးဘူး

Data ကို server ကနေ ယူတာကို setTimeout နဲ့ ပုံဖော်ထားတဲ့ code တစ်ခုပါ။ Developer က delay ကို 0 ms ထားလိုက်တော့ "ချက်ချင်း ပြီးမှာပဲ" လို့ ထင်ပြီး နောက် line မှာ user.name ကို ဖတ်ပါတယ်။ ရလဒ်က TypeError ပါ — user က null ဖြစ်နေဆဲပါ။ 0 ms ဆိုတာ "ချက်ချင်း" မဟုတ်ဘဲ "လက်ရှိ code ပြီးတာနဲ့ အမြန်ဆုံး" လို့ ဆိုလိုပါတယ်။

load-user.js
let user = null;

setTimeout(() => {
  user = { name: "Aye Aye" }; // pretend this came from a server
}, 0);

console.log(user.name);
// TypeError: Cannot read properties of null (reading 'name')

ဘာကြောင့် user က null ဖြစ်နေတာလဲ

setTimeout ကိုယ်တိုင်က callback ကို timer system ဆီ စာရင်းသွင်းပြီး ချက်ချင်း return ပြန်ပါတယ်။ Callback ကတော့ call stack ပေါ်က synchronous code အားလုံး ပြီးမှ run ခွင့်ရပါတယ်။ console.log(user.name) က အဲဒီ synchronous code ရဲ့ အစိတ်အပိုင်းဖြစ်လို့ callback မ run ခင် အရင် run သွားပါတယ်။ Async ရလဒ်ကို သုံးချင်ရင် callback ထဲမှာ ဒါမှမဟုတ် Promise ပြီးမှ သုံးရပါမယ်။

Core concept: call stack, queue နှစ်ခု နဲ့ event loop

JavaScript engine က call stack တစ်ခုတည်းပေါ်မှာ code ကို run ပါတယ်။ Function တစ်ခု ခေါ်တိုင်း stack ပေါ် တင်ပြီး ပြီးရင် ဖယ်ပါတယ်၊ တစ်ချိန်မှာ JavaScript အလုပ် တစ်ခုပဲ လုပ်နိုင်ပါတယ်။ ဒါပေမယ့် timer စောင့်တာ၊ network request ပို့တာ၊ file ဖတ်တာ၊ click event နားထောင်တာ စတဲ့ အလုပ်တွေကို engine ကိုယ်တိုင် မလုပ်ပါဘူး — browser (Web APIs) ဒါမှမဟုတ် Node.js (libuv) ဆိုတဲ့ runtime က stack ရဲ့ အပြင်ဘက်မှာ လုပ်ပေးပါတယ်။

Runtime က အလုပ်တစ်ခု ပြီးသွားရင် ဆက်လုပ်ရမယ့် callback ကို queue ထဲ ထည့်ပါတယ်။ Queue နှစ်မျိုး ရှိပါတယ်။ Task queue (macrotask queue လို့လည်း ခေါ်) မှာ timer callback၊ I/O callback နဲ့ event handler တွေ ဝင်ပါတယ်။ Microtask queue မှာတော့ Promise ရဲ့ .then() callback တွေ၊ await ပြီးနောက် ဆက်လုပ်ရမယ့် code နဲ့ queueMicrotask() တို့ ဝင်ပါတယ်။

Event loop က ရိုးရှင်းတဲ့ စည်းမျဉ်း တစ်ခုကို ထပ်ခါထပ်ခါ လုပ်ပါတယ်: call stack လွတ်သွားတာနဲ့ microtask queue ကို အကုန်ရှင်းပြီးမှ task queue ကနေ task တစ်ခု ယူ run ပါတယ်။ ဒါကြောင့် Promise reaction တွေက 0 ms timer ထက်တောင် အရင် run ပါတယ်။

text
The event loop
--------------
   +-------------+    hands off slow work    +---------------------+
   | Call stack  | ------------------------> | Runtime (Web APIs / |
   | (one thread)|                           | libuv): timers, I/O |
   +-------------+                           +---------------------+
          ^                                            |
          | event loop: when the stack is empty...     | when done
          |                                            v
   1. run ALL microtasks  <--  [ Promise .then, await, queueMicrotask ]
   2. then ONE task       <--  [ setTimeout, I/O, click handlers     ]

အရေးကြီးတဲ့ ကန့်သတ်ချက်က event loop က stack လွတ်မှပဲ queue ကို ကြည့်နိုင်တာပါ။ CPU အများကြီး သုံးတဲ့ synchronous loop တစ်ခု run နေရင် timer တွေ၊ click တွေ၊ browser ရဲ့ screen ပြန်ဆွဲတာတွေ အားလုံး စောင့်ရပါတယ်။ async ဒါမှမဟုတ် Promise ထဲ ထည့်ရေးရုံနဲ့ အဲဒီ loop က non-blocking မဖြစ်လာပါဘူး၊ တွက်ချက်မှု အကြီးကြီးတွေကို Web Worker ဒါမှမဟုတ် Node.js ရဲ့ worker_threads လို thread သီးခြားဆီ ပို့ရပါတယ်။ နောက်ထပ် နည်းလမ်းတစ်ခုက အလုပ်ကို အပိုင်းလေးတွေ ခွဲပြီး run တာပါ၊ ဒါမှ အပိုင်းတစ်ခုနဲ့ တစ်ခုကြားမှာ event loop က အသက်ရှူခွင့် ရပါတယ်။

မှတ်ထားရမယ့် ပုံစံ: event loop ရဲ့ တစ်ပတ်
  1. Synchronous code အားလုံး stack ပေါ်မှာ run
  2. Stack လွတ်ရင် microtask အားလုံး run
  3. Task queue ကနေ task တစ်ခု ယူ run
  4. ထပ်ခါထပ်ခါ ပြန်လည်

Delay ဆိုတာ အနည်းဆုံး စောင့်ချိန်ပါ၊ အတိအကျ အချိန် မဟုတ်ပါဘူး။

အထွေထွေ ပုံစံ

javascript
doSyncWork();                          // 1. runs now, on the stack

setTimeout(callback, delayMs);         // 4. task: after delay AND empty stack

promise.then(onResult);                // 3. microtask: right after sync code

queueMicrotask(() => { /* ... */ });   // 3. microtask as well

doMoreSyncWork();                      // 2. still part of the current run

ဘယ်ဘက်က code အစဉ်နဲ့ ညာဘက် comment ထဲက run တဲ့ အစဉ် မတူတာကို သတိထားပါ။ Synchronous code အားလုံး အရင်ပြီးပြီး microtask တွေ ဆက်လာကာ timer task က နောက်ဆုံးမှ ရောက်ပါတယ်။

Example: synchronous, microtask နဲ့ timer task တွေရဲ့ အစဉ်

ဒီ example မှာ "Start"၊ "End" နဲ့ 2 စက္ကန့် timer message အပြင် 0 ms timer နဲ့ Promise တစ်ခုကိုပါ ထည့်ထားလို့ queue နှစ်ခုရဲ့ ဦးစားပေး အစဉ်ကိုပါ မြင်ရပါမယ်။ Run မလုပ်ခင် output ငါးကြောင်းရဲ့ အစဉ်ကို ကြိုခန့်မှန်းကြည့်ပါ။

event-loop.js
console.log("Start");

// setTimeout hands the callback to the runtime's timer system
setTimeout(() => {
  console.log("Timer: shown after about 1 second");
}, 1000);

// Even a 0 ms timer must wait until the call stack is empty
setTimeout(() => {
  console.log("Timer: 0 ms, but still after End");
}, 0);

// Promise reactions are microtasks: they run before timer tasks
Promise.resolve().then(() => {
  console.log("Microtask: promise reaction");
});

console.log("End");

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Line 1console.log("Start");

    Synchronous code ဖြစ်လို့ ချက်ချင်း run ပါတယ်။ Output ရဲ့ ပထမ line ပါ။

  2. Lines 4-6}, 1000);

    setTimeout က callback ကို timer system ဆီ ပေးပြီး ချက်ချင်း return ပြန်ပါတယ်။ setTimeout ကိုယ်တိုင်က စောင့်တာ မဟုတ်ပါဘူး၊ callback ကသာ အနည်းဆုံး 1 စက္ကန့် ကြာမှ task queue ထဲ ဝင်ပါတယ်။

  3. Lines 9-11}, 0);

    0 ms ဆိုပေမယ့် ဒီ callback လည်း task ပါ။ Stack ပေါ်က code အားလုံးနဲ့ microtask အားလုံး ပြီးမှ run ခွင့်ရလို့ "End" ရဲ့ နောက်မှာ ထွက်ပါတယ်။

  4. Lines 14-16Promise.resolve().then(() => {

    Promise က resolve ပြီးသားဖြစ်ပေမယ့် .then() callback ကို ချက်ချင်း မ run ပါဘူး၊ microtask queue ထဲ ထည့်ပါတယ်။ Stack လွတ်တာနဲ့ timer task တွေထက် အရင် run ပါတယ်။

  5. Line 18console.log("End");

    Source ထဲမှာ နောက်ဆုံးဖြစ်ပေမယ့် synchronous ဖြစ်လို့ output ရဲ့ ဒုတိယ line ပါ။ ဒီ line ပြီးမှ stack လွတ်ပြီး event loop က queue တွေကို ကြည့်ပါတယ်။

You should see
Start
End
Microtask: promise reaction
Timer: 0 ms, but still after End
Timer: shown after about 1 second
Event loop ရဲ့ ဆုံးဖြတ်ချက်
  1. Synchronous code ကို ပြီးအောင် run
  2. Call stack လွတ်ပြီလား စစ်
  3. Microtask queue ကို အကုန် ရှင်း
  4. Task queue ကနေ task တစ်ခု ယူ run

Microtask ရှိ

Run them all before any task

Microtask မရှိ

Run the next task, then loop again

Blocking: busy loop က timer ကို နောက်ကျစေတယ်

အောက်က code က 10 ms timer တစ်ခု ပေးပြီး 200 ms ကြာတဲ့ synchronous busy loop ကို run ပါတယ်။ Timer က 10 ms မှာ ပြည့်သွားပေမယ့် stack ပေါ်မှာ loop ရှိနေသရွေ့ callback က run ခွင့်မရပါဘူး။ Browser ထဲမှာဆိုရင် ဒီ 200 ms အတွင်း click, scroll, animation တွေ အားလုံး ရပ်နေပါမယ်။

blocking.js
const started = Date.now();

setTimeout(() => {
  const waited = Date.now() - started;
  console.log(`Timer asked for 10 ms, waited ${waited >= 200 ? "200+" : "under 200"} ms`);
}, 10);

while (Date.now() - started < 200) {
  // busy loop: keeps the call stack occupied for 200 ms
}
console.log("Busy loop finished");

နောက် lesson တွေဆီ: callback ကနေ async/await ဆီ

Async ရလဒ်ကို ကိုင်တွယ်နည်း သုံးမျိုး ရှိပါတယ်: callback (အဟောင်းဆုံး)၊ Promise နဲ့ async/await (ယနေ့ အသုံးများဆုံး)။ async/await က Promise ပေါ်မှာ တည်ဆောက်ထားတာဖြစ်ပြီး code ကို synchronous လို အပေါ်ကနေ အောက် ဖတ်လို့ ရစေပါတယ်။ await က function ကို ခဏ ရပ်ပေမယ့် thread ကို ပိတ်မထားပါဘူး — အောက်က output မှာ brewTea() စောင့်နေတုန်း "Meanwhile" line က run သွားတာကို ကြည့်ပါ။ Promise နဲ့ async/await ကို နောက် lesson နှစ်ခုမှာ အသေးစိတ် လေ့လာပါမယ်။

await-preview.js
function wait(ms) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}

async function brewTea() {
  console.log("Boil water");
  await wait(50); // pauses brewTea only, not the whole program
  console.log("Steep tea");
  await wait(50);
  console.log("Tea is ready");
}

brewTea().then(() => console.log("brewTea finished"));
console.log("Meanwhile: set the table");

လက်တွေ့ project တွေမှာ

Real project တွေမှာ ဘယ်လို သုံးလဲ

Web app တိုင်းက async ပါ — fetch() နဲ့ API ခေါ်တာ၊ button click ကို စောင့်တာ၊ animation frame တွေ ဆွဲတာ အားလုံး event loop ပေါ်မှာ အလုပ်လုပ်ပါတယ်။ Node.js server တစ်ခုက thread တစ်ခုတည်းနဲ့ request ထောင်ချီကို ကိုင်တွယ်နိုင်တာက database ဒါမှမဟုတ် file ကို စောင့်နေတုန်း thread ကို ပိတ်မထားဘဲ နောက် request ကို ဆက်လက်ခံလို့ပါ။ ဒါပေမယ့် request handler တစ်ခုထဲမှာ ကြီးမားတဲ့ JSON ကို parse လုပ်တာ၊ password hash ကို synchronous တွက်တာလို CPU အလုပ်တွေ ပါရင် user အားလုံးအတွက် server တစ်ခုလုံး နှေးသွားပါတယ်။

Debug လုပ်တဲ့အခါ event loop အစဉ်ကို နားလည်ထားတာက အရမ်း အသုံးဝင်ပါတယ်။ "ဘာကြောင့် ဒီ log က အစဉ်မှား ထွက်နေတာလဲ"၊ "ဘာကြောင့် state က update မဖြစ်သေးတာလဲ" ဆိုတဲ့ မေးခွန်းတွေရဲ့ အဖြေက များသောအားဖြင့် microtask နဲ့ task အစဉ်ပါ။ Browser DevTools ရဲ့ Performance tab မှာ long task (50 ms ထက်ကြာတဲ့ task) တွေကို အနီရောင်နဲ့ ပြပေးလို့ page ကို ပိတ်ဆို့နေတဲ့ code ကို ရှာလို့ ရပါတယ်။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. Async ရလဒ်ကို နောက် line မှာ ချက်ချင်း သုံးတာ

Callback ထဲမှာ set လုပ်တဲ့ variable ကို setTimeout, fetch ခေါ်ပြီး နောက် line မှာ ဖတ်ရင် value မရောက်သေးပါဘူး။ ရလဒ်ကို သုံးတဲ့ code ကို callback ထဲ၊ .then() ထဲ ဒါမှမဟုတ် await ရဲ့ နောက်မှာ ရေးပါ။

2. Timer delay ကို အတိအကျ အချိန်လို့ ထင်တာ

setTimeout(fn, 1000) က "အနည်းဆုံး 1000 ms" လို့ပဲ ကတိပေးပါတယ်။ Stack အလုပ်များနေရင်၊ browser tab နောက်ခံမှာ ရောက်နေရင် ပိုကြာနိုင်ပါတယ်။ Countdown timer လို အချိန်တိကျဖို့ လိုရင် Date.now() နဲ့ တကယ် ကုန်သွားတဲ့ အချိန်ကို တွက်ပါ။

3. async ထည့်ရေးရင် လေးတဲ့ code က မပိတ်ဆို့တော့ဘူးလို့ ထင်တာ

async function ထဲမှာ loop သန်းချီ run ရင်လည်း အဲဒီ loop က main thread ပေါ်မှာပဲ run ပြီး page ကို ပိတ်ဆို့ပါတယ်။ await က စောင့်နေတဲ့အချိန်ကိုပဲ ပြန်ပေးနိုင်ပြီး တွက်ချက်မှုကို မမြန်စေပါဘူး။ Web Worker ဒါမှမဟုတ် worker_threads ကို သုံးပါ။

Mini challenge

Mini challenge: run တဲ့ အစဉ်ကို ကြိုခန့်မှန်းပါ

Server ကနေ user ကို 50 ms ကြာမှ ပြန်ပေးတဲ့ fakeFetchUser(id, callback) ကို ရေးပါ။ အဲဒါနဲ့အတူ microtask တစ်ခု၊ 0 ms timer တစ်ခု၊ synchronous log နှစ်ခု ပါတဲ့ program ကို ရေးပြီး log line တွေကို run မယ့် အစဉ်အတိုင်း 1 ကနေ 5 ထိ နံပါတ်တပ်ပါ။

  • fakeFetchUser က setTimeout 50 ms နဲ့ callback({ id, name }) ကို ခေါ်ရမယ်
  • Request ပို့ပြီးကြောင်း log ကို fakeFetchUser မခေါ်ခင် synchronous ထုတ်ရမယ်
  • Promise.resolve().then(...) တစ်ခုနဲ့ setTimeout(..., 0) တစ်ခု ထည့်ရမယ်
  • Run လိုက်ရင် log တွေ 1, 2, 3, 4, 5 အစဉ်အတိုင်း တိတိကျကျ ထွက်ရမယ်
run-order.js
function fakeFetchUser(id, callback) {
  // TODO: after 50 ms, call callback with { id, name }
}

// TODO: "1. request sent" (synchronous)
// TODO: call fakeFetchUser(1, ...) and log "5. user loaded: <name>"
// TODO: setTimeout(..., 0) that logs "4. timeout 0 ran"
// TODO: Promise.resolve().then(...) that logs "3. microtask ran"
// TODO: "2. still running synchronous code"

ရလဒ်: Log တွေ နံပါတ်အစဉ်အတိုင်း ထွက်ရပါမယ်: 1. request sent 2. still running synchronous code 3. microtask ran 4. timeout 0 ran 5. user loaded: Aye Aye

အရိပ်အမြွက် ကြည့်ရန်

Source ထဲမှာ ရေးတဲ့ အစဉ်နဲ့ run တဲ့ အစဉ် မတူပါဘူး။ Synchronous log တွေ အရင်၊ microtask က ဒုတိယ၊ 0 ms timer က တတိယ၊ 50 ms timer က နောက်ဆုံး ဆိုတဲ့ စည်းမျဉ်းအတိုင်း နံပါတ်တပ်ပါ။

အဖြေ ကြည့်ရန်
run-order.js
function fakeFetchUser(id, callback) {
  setTimeout(() => {
    callback({ id, name: id === 1 ? "Aye Aye" : "Unknown" });
  }, 50);
}

console.log("1. request sent");
fakeFetchUser(1, (user) => console.log(`5. user loaded: ${user.name}`));
setTimeout(() => console.log("4. timeout 0 ran"), 0);
Promise.resolve().then(() => console.log("3. microtask ran"));
console.log("2. still running synchronous code");

Quick Quiz 1 / 3

console.log("A"); setTimeout(() => console.log("B"), 0); Promise.resolve().then(() => console.log("C")); console.log("D"); ရဲ့ output အစဉ်က ဘာလဲ။

Quick Quiz 2 / 3

setTimeout(fn, 1000) က ဘာကို အာမခံလဲ။

Quick Quiz 3 / 3

Browser မှာ button click handler ထဲက 3 စက္ကန့်ကြာ synchronous loop တစ်ခု run နေတုန်း ဘာဖြစ်မလဲ။

မှတ်ထားရမယ့် အချက်များ

  • JavaScript က call stack တစ်ခုတည်းပေါ်မှာ run ပြီး ကြာတဲ့ အလုပ်တွေကို runtime (Web APIs / libuv) ဆီ လွှဲပေးပါတယ်။
  • Event loop က stack လွတ်မှ microtask အားလုံးကို အရင် run ပြီး task တစ်ခုကို ယူ run ပါတယ်။
  • setTimeout(fn, 0) ကို synchronous code နဲ့ Promise reaction တွေ ပြီးမှ run ပြီး delay က အနည်းဆုံး စောင့်ချိန်ပဲ ဖြစ်ပါတယ်။
  • CPU အလုပ်လေးတွေက event loop ကို ပိတ်ဆို့လို့ async ထည့်ရုံနဲ့ မဖြေရှင်းနိုင်ပါဘူး၊ worker ကို သုံးပါ။

MDN — Asynchronous JavaScript — MDN Web Docs

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Async JS Intro | Thuta Learning