when() से कई Async Manage करना
In this page:
$.when(promise1, promise2).done(function (result1, result2) {
// all resolved
});
when() क्या है?
$.when() एक या ज़्यादा Deferred objects या promises लेता है और एक single combined promise return करता है जो सिर्फ तभी settle होता है जब सब settle हो चुके हों।
उदाहरण: What is when()?
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
</head>
<body>
<script>
const a = $.Deferred();
const b = $.Deferred();
$.when(a, b).done(function() { console.log("Both settled"); });
a.resolve();
b.resolve();
</script>
</body>
</html>
AJAX के साथ when
when() कई asynchronous requests को combine करने के लिए खासतौर पर उपयोगी है — मान लीजिए, एक page बनाने के लिए ज़रूरी तीन अलग AJAX calls — ताकि final call।
उदाहरण: when with AJAX
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
</head>
<body>
<script>
$.when($.get("data.php"), $.get("data.php")).done(function(r1, r2) {
console.log("Both requests finished");
});
</script>
</body>
</html>
Failure Handle करना
अगर combined operations में से कोई एक भी fail हो जाए, तो combined promise पर done() के बजाय fail() callback चलता है, जो तब उपयोगी है जब overall task।
उदाहरण: Handle Failure
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
</head>
<body>
<script>
const a = $.Deferred();
const b = $.Deferred();
$.when(a, b).fail(function() { console.log("At least one failed"); });
a.resolve();
b.reject();
</script>
</body>
</html>
अलग-अलग Delays इस्तेमाल करना
when() को pass की गई individual operations complete होने में अलग-अलग समय ले सकती हैं, और when() resolve होने से पहले सबसे slow वाली का wait करेगा।
उदाहरण: Use Different Delays
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
</head>
<body>
<script>
const fast = $.Deferred();
const slow = $.Deferred();
setTimeout(function() { fast.resolve(); }, 50);
setTimeout(function() { slow.resolve(); }, 300);
$.when(fast, slow).done(function() { console.log("Both finished, waited for slowest"); });
</script>
</body>
</html>
एक छोटा when() Project
when() उन pages पर खासतौर पर valuable है जिन्हें data के कई independent pieces चाहिए — जैसे एक user profile, उनके recent orders, और site settings।
उदाहरण: Small when() Project
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
</head>
<body>
<script>
$.when($.get("data.php"), $.get("data.php"), $.get("data.php")).done(function() {
console.log("Profile, orders, and settings all loaded");
});
</script>
</body>
</html>
- promises के बजाय functions call करने के results pass करना, या कोई ऐसी value जो promise नहीं है, जबकि
$.when()को Deferred objects चाहिए जैसे$.when($.get(...), $.get(...))। - AJAX calls के लिए
done()से plain values की उम्मीद करना, जबकि हर argument एक array है जैसे[data, status, jqXHR]। - यह मान लेना कि
fail()सभी tasks का wait करता है, जबकि यह एक के fail होते ही fire हो जाता है और बाकियों का wait नहीं करता।
- Deferred objects उस काम को represent करते हैं जो बाद में खत्म होता है, pending, resolved, और rejected जैसी states के साथ।
- आप deferreds बनाते और resolve करते हैं और promise pipeline methods से steps chain करते हैं।
when()कई asynchronous tasks को एक साथ manage करता है।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: