Async / Await
JavaScript async await se promises ko seedhe aur readable code mein likhna, try catch, parallel await aur top-level await, Roman Urdu mein.
On this page
1. Async / Await kya hai?
async function hamesha ek promise return karta hai. Uske andar await likh kar aap kisi promise ke settle hone ka intezar karte hain, aur result seedha variable mein mil jata hai.
await sirf us function ko rokta hai, poore program ko nahi. Baqi page aur code chalta rehta hai.
2. Kyun zaroori hai?
.then chains lambi hon to padhna mushkil ho jata hai, khaas kar jab conditions aur loops hon. async / await wahi logic upar se neeche seedha likhne deta hai.
| then chain | async / await |
|---|---|
| then ke andar then | Upar se neeche seedhi lines |
| Variables ek step se doosre mein le jana mushkil | Normal const variables |
| catch alag method | Jana pehchana try catch |
| Loops ke saath async mushkil | for...of ke andar await asaan |
3. Kahan use hota hai?
| Jagah | Example |
|---|---|
| API calls | const res = await fetch(url) |
| Form submit | Submit handler ko async bana kar data bhejna |
| Node.js scripts | Files padhna, database queries |
| Next.js / React Server Components | async components mein data fetch |
| ES modules | Top-level await config load karne ke liye |
4. Kaise kaam karta hai?
| Agar promise | await kya karta hai |
|---|---|
| Fulfilled | Value return karta hai |
| Rejected | Error throw karta hai, try / catch pakarta hai |
5. Code example
Basic async function with try catch
async function loadUser(id) {
try {
const res = await fetch(`https://jsonplaceholder.typicode.com/users/${id}`);
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const user = await res.json();
return user.name;
} catch (error) {
console.error("Load fail:", error.message);
return null;
} finally {
console.log("Request khatam");
}
}
const name = await loadUser(1);
console.log(name);Sequential vs parallel
const slowA = () => new Promise((r) => setTimeout(() => r("A"), 1000));
const slowB = () => new Promise((r) => setTimeout(() => r("B"), 1000));
async function sequential() {
const a = await slowA();
const b = await slowB();
return [a, b];
}
async function parallel() {
const [a, b] = await Promise.all([slowA(), slowB()]);
return [a, b];
}Code kaise kaam kar raha hai?
| Line | Kya ho raha hai |
|---|---|
async function loadUser | Function ab promise return karta hai |
await fetch(...) | Response aane tak function ruka |
if (!res.ok) throw | HTTP error ko khud error banaya |
catch (error) | Network ya thrown error yahan aata hai |
sequential() | Takreeban 2 second, ek ke baad ek |
parallel() | Takreeban 1 second, dono saath |
Interview tip
async function se return ki gayi value bhi promise mein lipat jati hai. Is liye loadUser(1) ka result string nahi, Promise hai, aur use await ya .then chahiye.
6. Common mistakes
Galati: await bhool jana
Why? Variable mein data ki jagah pending Promise aata hai aur user.name undefined hota hai.
Behtar: Promise return karne wali har call ke aage await lagayein jab result chahiye.
Galati: forEach ke andar await
Why? forEach async callback ka intezar nahi karta, code aage nikal jata hai.
Behtar: Order chahiye to for...of loop, parallel chahiye to Promise.all with map.
Galati: Independent awaits ko line se likhna
Why? Har request pichli ka intezar karti hai, page slow ho jata hai.
Behtar: Jo kaam ek doosre par depend nahi karte unhein Promise.all mein chalayein.
Galati: try catch ke bina await
Why? Rejected promise unhandled error ban jata hai.
Behtar: await ko try catch mein rakhein ya caller par catch lagayein.
7. Pehle aur baad mein kya seekhein?
| Stage | Topics |
|---|---|
| Pehle | Promises, Event Loop |
| Baad mein | Fetch API, Error Handling, Modules |
Final quick revision
| Point | Detail |
|---|---|
async | Function hamesha promise return karta hai |
await | Promise ka result nikalta hai, sirf us function ko rokta hai |
| Errors | Rejected promise throw banta hai, try / catch se pakrein |
| Parallel | await Promise.all([...]) |
| Loops | for...of ke saath await, forEach ke saath nahi |
| Top-level await | ES modules mein function ke bina bhi chalta hai |