Welcome to our deep dive into AJAX Promises/Deferred using jQuery! In this lesson, we'll explore how to make asynchronous requests, handle responses, and chain multiple AJAX operations with ease. Let's get started! 📝
AJAX Promises (also known as Promise objects) are JavaScript objects that represent the eventual completion or failure of an asynchronous operation. They provide a cleaner, more readable way to handle AJAX callbacks and errors.
Deferred objects are simply a specialized type of Promise that gives us more control over the asynchronous operation and allows us to chain multiple AJAX calls together.
Before we dive into Promises and Deferred objects, let's review how to set up a basic AJAX request using jQuery:
$.ajax({
url: "example.php",
dataType: "json",
success: function(data) {
// Handle success
},
error: function(jqXHR, textStatus, errorThrown) {
// Handle errors
}
});In the example above, we're making an AJAX request to a PHP script (example.php) and expecting JSON data in response. We've also provided success and error callbacks to handle the response and any errors that may occur.
Now, let's create a Deferred object:
var deferred = $.Deferred();A Deferred object contains two important properties: promise and resolve. The promise property is the Promise object we're interested in, while resolve is a function we can call to mark the Deferred object as complete and pass a result value.
Now that we have a Deferred object, let's modify our AJAX request to use it:
var deferred = $.Deferred();
$.ajax({
url: "example.php",
dataType: "json",
success: function(data) {
deferred.resolve(data); // Mark the Deferred object as complete
},
error: function(jqXHR, textStatus, errorThrown) {
deferred.reject("An error occurred!"); // Mark the Deferred object as failed and pass an error message
}
});
// We can now use the Deferred object's promise to handle the response:
deferred.promise().done(function(data) {
console.log("Data received:", data);
}).fail(function(error) {
console.error("Error occurred:", error);
});In this example, we've created a Deferred object and modified our AJAX request to call resolve or reject depending on the success or failure of the request. We then use the promise property to handle the response and any errors.
Now that we know how to use Deferred objects, let's chain multiple AJAX requests together:
var firstRequest = $.Deferred();
var secondRequest = $.Deferred();
firstRequest.done(function(data) {
// Perform some operation on the data and pass the result to the second request
secondRequest.resolve(processData(data));
});
firstRequest.fail(function(error) {
secondRequest.reject(error);
});
secondRequest.done(function(data) {
console.log("Data received from second request:", data);
});
secondRequest.fail(function(error) {
console.error("Error occurred in second request:", error);
});
// Kick off the first request
firstRequest.promise().then(function() {
// The first request has completed. Now kick off the second request
secondRequest.promise().then(function(data) {
// The second request has completed
});
});In this example, we have two Deferred objects representing two AJAX requests. The first request's done callback processes the data and passes the result to the resolve function of the second request's Deferred object. If the first request fails, the error is passed to the second request's reject function. We then kick off the requests using the promise property and handle the response and errors using the then method.
What is a jQuery Deferred object used for?
That's it for today! In the next lesson, we'll dive deeper into AJAX Promises and learn how to use them with jQuery's $.when function and handle multiple concurrent AJAX requests. Until then, keep coding and learning! 🎯