10/05/2025
JavaScript ရဲ့ `using` နဲ့ `await using` Syntax (ECMAScript Explicit Resource Management)
အဲ့ Syntax နှစ်ခုက ECMAScript Explicit Resource Management proposal ဆိုတဲ့ ခေါင်းစဉ်အောက်မှာ introduce လုပ်ထားတဲ့ feature တစ်ခုပါ။
ဒီ using ဆိုတဲ့ Syntax ကို I/O Operation တွေ Socket နဲ့ တခြား resources တွေလိုမျိုးတွေ auto-close ဖို့ သုံးနိုင်ပါတယ်။
using နဲ့ await using ဆိုတဲ့ syntax တွေက C #, Java, and Python တို့လိုမျိုး scoped လုပ်ထားတဲ့ resource တွေကို structured cleanup တဲ့ Syntax တွေပဲဖြစ်ပါတယ်။
Automatically disposes of resources (calls Symbol.dispose or Symbol.asyncDispose) at the end of the block.
using နဲ့ await using တွေက synchronous အတွက်ဆိုရင် Symbol.dispose ကိုခေါ်ပြီးတော့ asynchronous အတွက်ဆိုရင် Symbol.asyncDispose ကိုခေါ်ပြီး cleanup လုပ်ပါတယ်။
JavaScript မှာ resource lifetime တွေကို clean up လုပ်ဖို့အတွက် method တွေအများကြီးရှိလာနိုင်ပါတယ်။ .close(), .releaseLock(), .return() ဆိုပြီးတော့ပေါ့
ဒါပေမဲ့ using syntax ကိုသုံးမယ်ဆိုရင်တော့ Automatic Clean Up ဖြစ်မှာဖြစ်ပြီးတော့ code က လည်း clean နေမှာပါ။
using syntax ကိုမသုံးရင်ရှုပ်နေမှာပါ။
const a = getA();
try {
const b = getB();
try {
...
} finally {
b.dispose();
}
} finally {
a.dispose();
}
using ကိုသုံးလိုက်မယ်ဆိုရင်တော့
using a = getA(), b = getB(); // a နဲ့ b ကို block scope မှာ automatically dispose လုပ်တယ်
using က block လုံးမှာ resource တွေကို manage လုပ်ပြီး, block က end အကုန် resource တွေကို dispose လုပ်သွားတယ်
ဒီ using syntax က error expection ဖြစ်ခဲ့မယ်ဆိုရင်တောင် automatically dispose လုပ်ဖို့ grantee ထားပါတယ်။
ဒီ feature က browsers တွေအကုန်လုံးမှာတော့ available မဖြစ်သေးပါဘူး ဒါပေမဲ့ ကျွန်တော့် Chrome Version 136.0.7103.93 နဲ့ V8 13.6.233.8 မှာ တော့ သုံးလို့ရနေပါပြီ
Learn More: https://github.com/tc39/proposal-explicit-resource-management?tab=readme-ov-file