JS एसिंक कॉलबैक्स
In this page:
function functionName(callback) {
// async work
callback(result);
}
functionName(function(result) {
// use result
});
Callback क्या है
एक callback बस एक regular function है जो किसी दूसरे function को argument के रूप में पास किया जाता है -- receiving function उसे बाद में किसी point पर वापस कॉल करता है, अक्सर किसी asynchronous operation (जैसे कोई timer या network request) के खत्म होने पर।
उदाहरण: What a Callback Is
// Define the function `greet` taking `name`, `callback`
// Define the function `greet` taking `name`, `callback`
function greet(name, callback) {
// Call `callback(`Hello, ${name}`)`
// Call `callback(`Hello, ${name}`)`
callback(`Hello, ${name}`);
}
// Call `greet("Sam", (message) => console.log(message))`
// Call `greet("Sam", (message) => console.log(message))`
greet("Sam", (message) => console.log(message));
Asynchronous Operations के साथ Callbacks
callback का classic इस्तेमाल इसे किसी ऐसी चीज़ के साथ जोड़ना है जिसमें समय लगे -- setTimeout(callback, delay) delay बीतने के बाद callback को कॉल करता है, और पुराने AJAX APIs किसी network response के आने पर code चलाने के लिए callbacks इस्तेमाल करते थे, क्योंकि "बाद में" होने वाली किसी चीज़ पर react करने का कोई और तरीका नहीं था।
उदाहरण: Callbacks with Asynchronous Operations
setTimeout(() => console.log("Runs later"), 500);
// Older AJAX used callbacks similarly, run once a response arrives
Error-First Callback Convention
एक व्यापक रूप से अपनाया गया convention, खासतौर पर Node.js-style code में, "error-first" callback है: callback(error, result), जहाँ पहला argument या तो एक Error object है (अगर कुछ गलत हुआ) या null (अगर सफल रहा) -- उस पहले argument को जांचना callback-based code में failure संभालने का standard तरीका बन जाता है।
उदाहरण: The Error-First Callback Convention
// Define the function `loadData` taking `callback`
// Define the function `loadData` taking `callback`
function loadData(callback) {
// Call `setTimeout(() => callback(null, { id: 1 }), 100)`
// Call `setTimeout(() => callback(null, { id: 1 }), 100)`
setTimeout(() => callback(null, { id: 1 }), 100);
}
loadData((error, result) => {
if (error) console.log("Failed:", error);
else console.log("Success:", result);
});
समस्या: Callback Hell
जब कई asynchronous steps क्रम में होने चाहिए, हर एक पिछले वाले के result पर निर्भर, तो callback-based code गहराई में और गहरा nest होता जाता है -- एक pattern जिसे आमतौर पर 'callback hell' या 'the pyramid of doom' कहा जाता है, जो और steps जुड़ने के साथ सच में पढ़ना, बदलना, और debug करना मुश्किल हो जाता है।
उदाहरण: The Problem: Callback Hell
function step1(cb) { setTimeout(() => cb(1), 10); }
function step2(a, cb) { setTimeout(() => cb(a + 1), 10); }
function step3(b, cb) { setTimeout(() => cb(b + 1), 10); }
step1((a) => {
step2(a, (b) => {
step3(b, (c) => console.log("Deeply nested result:", c)); // callback hell
});
});
Callbacks अभी भी इस्तेमाल में
ज़्यादातर sequential async logic के लिए Promises और async/await preferred होने के बावजूद, साधारण callbacks कुछ patterns के लिए आम और उपयुक्त बने हुए हैं -- event listeners (addEventListener), array iteration methods (forEach, map), और कोई भी 'हर बार X होने पर इसे चलाओ' वाला scenario जो असल में किसी single eventual result के बारे में नहीं है।
उदाहरण: Callbacks Still in Use Today
document.addEventListener("click", () => console.log("Clicked")); // still a normal, appropriate callback use
[1, 2, 3].forEach(n => console.log(n)); // another normal callback use
- dependent async steps की एक sequence के लिए callback के अंदर callback के अंदर callback nest करना, deeply indented, follow करने में मुश्किल code produce करते हुए जिसे अक्सर 'callback hell' कहा जाता है।
- error-first callback convention inconsistently follow करना, result argument इस्तेमाल करने से पहले error argument check करना भूल जाना।
- किसी callback function को reference pass करने के बजाय उसे तुरंत (parentheses के साथ) कॉल करना, उसे बाद के लिए schedule करने के बजाय उसे तुरंत execute करते हुए।
- एक callback बस एक ऐसा function है जो किसी दूसरे function को argument की तरह pass किया जाता है, किसी operation के पूरा होने पर बाद में invoke किया जाने के लिए।
- error-first convention (callback(error, result)) एक common pattern है जहाँ पहला argument या तो एक error object होता है या null।
- sequential async steps के लिए deeply nested callbacks एक हार्ड-टू-रीड "callback hell" produce करते हैं, जिसे solve करने के लिए Promises और async/await design किए गए थे।
callback pattern खुद एक basic JavaScript language feature है, जिसे language के बनने से support मिला है, और यह हर environment में काम करता है।
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS Dates
- JS Math
- JS Conditionals
- JS Switch
- JS Loop For
- JS Loop While
- JS Iterables
- JS Sets
- JS Maps
- JS typeof
- JS Type Conversion
- JS Destructuring
- JS Arrow Functions
- JS Classes
- JS Modules
- JS Promises
- JS Async/Await
- JS DOM
- JS DOM Methods
- JS Events Advanced
- JS DOM Navigation
- JS DOM Collections
- JS Async Callbacks
- JS Async Parallel
- JS Date Set
- JS Set Logic