What is async / await in JavaScript in Hindi

what is async await in JavaScript in hindi

Introduction

हेलो दोस्तों आज हम इस post में javascript के async/await method के बारे में जानेगे. Javascript में हम async/await method का इस्तेमाल करके कैसे asynchronous operations को perform करा सकते है!. तो हम इस post में async/await क्या है और वह कैसे काम करता है और इसका इस्तेमाल हम अपने program में कैसे कर सकते है वह हम example के साथ समझेंगे!.

Javascript program कैसे काम करता है?

Javascript program async / await method को समझने से पहले हम जान लेते है की javascript का program यानि code कैसे execute होता है!. तो javascript में program step by step यानि line by line execute होता है तो हम इसे कह सकते है की javascript एक synchronous programing language है!. javascript में जब तक एक code की line पूरी तरह execute नहीं हो जाती तब तक वह दूसरे line का code को execute नहीं करेंगे चाहे उसके लिए time बी लग जाये और इसी  कारन से हमारे user/customer experiance ख़राब हो जाता है!. javascript में ऐसे code है जो किसी API calls, database से data get करने में time लेते है ऐसे task तो perform करने के लिए हम asynchronous operations perform करा सकते है जो हमारे code execution को block नहीं करते है लेकिन asynchronous operation का response आने तक javascript आगे का code को execute करता है जब response मिल जाता है तो फिरसे वह operation यानि task perform करने लगता है!. और asynchronous operation perform करने के लिए javascript में specific  method/ keyword के जरिये  javascript को पता चलता है की वह asynchronous operation है और वह keyword मेसे async / await बी एक keyword है!.

Javascript में async / await क्या है?

async/await JavaScript programming में asynchronous operations को handle करने का एक modern और easy-to-use method है!. इसका इस्तेमाल तब किया जाता है जब आपको time-consuming operations को रन करना हो जैसे की API call, file reading, database queries जो बिना poori application website को block किये!.

जब हम javascirpt में synchronous code लिखते हैं, तो वह एक line का काम पूरा होने के बाद ही अगली लाइन execute होती है!.
अगर कोई operation या task ज़्यादा time लेता है जैसे किसी third party से data get करने की, network request की ho, तो उसके complete होने तक code block हो जाता है!. और वह हमारे user experience को slow बना सकती है!. तो हमारी applicaiton और website slow बी न हो और kam बी हो जाये इसके लिए हम Asynchronous programming के माध्यम से हम ऐसे long-running tasks को background में perform कर सकते हैं और बाकि का code तुरंत execute कर सकते हैं!.

async/await का Syntax

async Function: जब किसी function को async keyword के साथ define किया जाता है, वो हमेशा एक Promise return करता है. इसका फायदा ये है की आपको manually Promise create करने की ज़रूरत नहीं पड़ती!.
await Keyword: इसका उसे सिर्फ async functions के अंदर होता है. ये किसी asynchronous operation के result का wait करता है बिना पूरी application को block किये!.

Example:

<script>
function fetchData() {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve("Data fetch after 2 second");
    }, 2000);
  });
}

async function getData() {
  console.log("Data fetching is in processing...");
  const result = await fetchData();

  console.log(result);
}
getData();

</script>

ऊपर के example में देख सकते है की getData() funciton को async keyword के साथ declare किया गया है, इसलिए यह एक Promise return करेगा!. और उसके बाद में हमे पता ही है await को हम async वाले function के बितर में define कर सकते है तो
await fetchData() यह ensure करता है की code तब तक wait करे जब तक fetchData() का result नहीं आ जाता,
बिना पूरी application को block किये. यह asynchronous operation को synchronous जैसा flow deta है, जो code को ज़्यादा readable बनता है!.

async/await के साथ error को handle कैसे करा जाता है?

async/await के साथ error handling को try/catch block के through मैनेज किया जाता है, जो synchronous code जैसा दिखाई  देता है!.

Example:

<script>
  function fetchData() {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
       resolve("Data fetch after 2 second");
      }, 2000);
    });
  }

  async function getData() {
    try {
      const result = await fetchData();
      console.log(result);
    } catch (error) {
      console.log("Error:", error);
    } 
 }
 getData();
</script>

ऊपर के example में देख सकते है हमने error को hanle करने के लिए try-catch का उसे किया है!. try block का उसे होता है ऐसे  code को लिखने के लिए जो error generate कर सकता हो!. यहाँ, try block के अंदर asynchronous function fetchData() को call किया गया है, जो शायद data को fetch करने का काम करेगा!.
catch (error) block अगर fetchData() function reject होता है तो try block से error throw होता है, और catch block में आता है!. catch (error) block का काम है उस error को handle करना. console.log(“Error:”, error); के through error को print किया जाता है, जिससे developer को पता चले की क्या गलत हो रहा है!.

async/await asynchronous tasks को manage करने का एक modern और preferred method है क्युकी यह code को more readable, clean, और manageable बनता है!।

Leave a Comment

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