Thuta Learning
ရှာဖွေရန်
AdvancedProgrammingbeginner

Async / Await

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

Async/Await သည် Promises များကို ပိုမိုရှင်းလင်းပြီး synchronous code ကဲ့သို့ ဖတ်ရလွယ်ကူအောင် ရေးသားနိုင်သော syntax ဖြစ်သည်။

async: Function တစ်ခုသည် promise ကို return ပြန်ပေးကြောင်း ကြေညာရန်

await: Promise တစ်ခု resolve ဖြစ်သည်အထိ စောင့်ဆိုင်းရန် (async function ထဲမှာသာသုံးနိုင်သည်)

javascript
function resolveAfter2Seconds() {
  return new Promise(resolve => {
    setTimeout(() => resolve('resolved'), 2000);
  });
}

async function asyncCall() {
  console.log('calling');
  const result = await resolveAfter2Seconds();
  console.log(result); // "resolved"
}

asyncCall();
You should see
calling (2 seconds later) resolved
Async / Await | Thuta Learning