Don’t forget callback

သောကြာနေ့က Server Side ပိုင်းကို prototype ကို Node.js သုံးပြီး ရေးထားတာကို real data နဲ့ စမ်းကြည့်တော့ ပြဿနာ တက်နေတယ်။ Request ပို့လိုက်ပေမယ့် respond က ပြန်မလာဘူး ဖြစ်နေတယ်။ Test data နဲ့ စမ်းတာကတော့ အကုန် အဆင်ပြေတယ်။

ရေးထားတာက

callback တွေကို ဆင့်ကာ ဆင့်ကာ ခေါ်ထားတာကို တွေ့ရမှာပါ။ Node.js မှာ callback တွေ ဆင့်ကာ မခေါ်ချင်ရင် event commit လုပ်လို့ရပါတယ်။ Async ဖြစ်တဲ့အတွက်ကြောင့် sync လိုမျိုး line bye line ပုံစံ မျိုးထက် ပိုစဉ်းစားရခက်တယ်။ non-blocking, asynchronous architecture ဖြစ်တဲ့ အတွက်ကြောင့် Database က နေ data တစ်ခုခြင်းစီ ထုတ်လိုက်ရင် တစ်ခါနဲ့ တစ်ခါ result order တွေ မတူတာတွေ ရှိတတ်တယ်။

အခုလိုမျိုး အဆင့်ဆင့် စစ်ဆေးရတဲ့ ကိစ္စတွေမှာ အရင်ပြီးတာ အရင်ထွက်လာတယ်။ ဟို အရင်ကလိုမျိုး အရင်လာတာ အရင် တွက် မဟုတ်တော့တဲ့ အတွက်ကြောင့် ပိုပြီးမြန်လာတယ်။ သို့ပေမယ့် CPU က 1 processor နောက်ပြီး nginx worker က တစ်ခုပဲ ဆိုရင်တော့ ဘာမှ မထူးပါဘူး။ တကယ်လို့ 2 processor မှာ 4 worker လောက်နဲ့ run လိုက်ရင်တော့ အရမ်းကို သိသာသွားတယ်။

callback တွေ ဆင့်ကာ ဆင့်ကာ ခေါ်ရတဲ့ အချိန်မှာ ပြဿနာလေးတွေ ရှိတတ်တယ်။ တစ်ခါတစ်လေ callback ခေါ်ဖို့ ကျန်ခဲ့တာဆိုရင်တော့ သေပြီပဲ။ အပေါ်က code မှာ များနေတာ ရှိပါတယ်။ အဲဒါကတော့ for loop ကို တန်းခေါ်ထားပြီး array count ကို မစ်ထားတာပဲ။ တကယ်လို့ အဲဒီ လူက phone number သာမရှိရင် callback က return ပြန်တော့မှာ မဟုတ်တော့ဘူး။

ဖြစ်သင့်တာက အဲဒီလို ကိစ္စမျိုးမှာ loop မဝင်ခင်မှာ length ကို အရင် စစ်ပြီးတော့ မရှိဘူးဆိုရင် callback ပြန်သင့်ပါတယ်။

အဲဒီ တစ်ကြောင်းလေးအတွက် ကျွန်တော့်မှာ အချိန် ၂ နာရီလောက် ပေးလိုက်ရတယ်။

Expressjs ကို သုံးပြီး module တွေ ခွဲရေးရင် callback ကို ပြန်ခေါ်ဖို့ အတွက် ပိုစဉ်းစားဖို့ လိုတယ်။ နောက်ပြီး အရင်ကလို ဒါပြီး ဒါလာ ဆိုပြီး ခေါ်လို့မရတော့ဘူး။

ဘာ ပိုကောင်းသလဲဆိုရင်တော့ data တွေ insert ထည့်တဲ့ အခါ နောက်ပြီး result တွေ အများကြီးကို loop ပတ်ပြီး တစ်ခုခြင်းစီ စစ်ရတဲ့ ကိစ္စတွေမှာ ရေးရတာ ပိုရှင်းတယ်။ လွန်ခဲ့တဲ့ ၁ နှစ် ကျော်ကျော်လောက်က prototype တစ်ခု ဆောက်ဖူးသေးတယ်။ သို့ပေမယ့် Node.js ကို အဲဒီတုန်းက လက်လျော့လိုက်ရတယ်။ Async ကို ဘယ်လိုမှ မစဉ်းစားတတ်လို့။ sync စဉ်းစားပုံ အတိုင်း တစ်ဆင့်ခြင်း ဆီ စဉ်းစားပေမယ့် မရတာတွေ ရှိတယ်။

အခုအချိန်မှာတော့ PHP ထက် Node.js ကို ပိုသဘောကျနေတာ အမှန်ပဲ။ MySQL ထက် Mongodb ကို ပိုသဘောကျတယ်။ prototype အသေးစားလေးတွေ ကို ဖန်တီးရာမှာ Node.js + mongodb က မြန်မြန်ဆန်ဆန် လေး ရေးလို့ရတာကြောင့်လည်း ပါတာပေါ့။

4 Comments

  1. Ye Myat Min says:

    Regarding Async flow, this library has helped me a lot.
    Asyncjs – https://github.com/caolan/async

    You should definitely check it out. It includes really useful controls like parallels, waterfall and etc.

    There are also similar libraries (e.g. Step) but Async is the most comprehensive in my opinion.

    Step – https://github.com/creationix/step

    1. saturngod says:

      Oh! I forget about async library. A bit lazy to use external library :D

      1. Ye Myat Min says:

        Libraries were created to cure laziness in all of us, no? And you’re lazy to use a library? Hahha.

        1. saturngod says:

          the problem is I lazy to learn how to use :D

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.