What are deferred and promise object in jQuery?
In jQuery, Deferred and Promise objects are used for handling asynchronous operations, such as AJAX requests, animations, or any other tasks that may take time to complete. These objects provide a way to manage and coordinate such operations, making it easier to work with asynchronous code and handle the results when they are available.
Deferred Object
- A Deferred object represents a value (or a sequence of values) that may not be available yet but will be at some point in the future.
- It provides methods like .resolve(), .reject(), and .notify() to control the state and progress of an asynchronous operation.
- You can attach callbacks to a Deferred object using .done(), .fail(), and .progress(). These callbacks are executed when the operation is successful, encounters an error, or reports progress, respectively.
function fetchData() {
var deferred = $.Deferred();
// Simulate an AJAX request
setTimeout(function() {
var data = { message: "Data fetched successfully" };
deferred.resolve(data);
}, 2000);
return deferred.promise();
}
var promise = fetchData();
promise.done(function(data) {
console.log(data.message); // Output: Data fetched successfully
});
Promise Object
- A Promise object represents the eventual result of an asynchronous operation and is returned by a Deferred object's .promise() method.
- While you can't directly change the state or progress of a Promise, you can attach callbacks using .then(), which is similar to .done() and .fail().
- Promises help make asynchronous code more readable and manageable, allowing you to chain operations and handle success or failure in a clean way.
function fetchData() {
return $.ajax({
url: "https://example.com/api/data",
method: "GET"
});
}
fetchData()
.then(function(data) {
console.log(data); // Handle success
})
.catch(function(error) {
console.error(error); // Handle error
});
Conclusion
Deferred objects are used to create and control asynchronous operations, while Promise objects represent the eventual outcome of those operations. They help streamline the handling of asynchronous tasks, making code more readable and maintainable.
Related Topics
- jQuery Interview Questions (Part-2)
- jQuery Interview Questions (Part-3)
- Is jQuery a programming language?
- Why do we need to go for JQuery?
- How to check jQuery version?
- How to multiple version of jQuery?
- What is jQuery CDN?
- Advantages of minified version of JQuery
- How do I check if the DOM is ready?
- How to Use the jQuery load() Method
- Difference between document.ready() and body onload()?
- Is jQuery is a replacement of JavaScript?
- JQuery or JavaScript which is quicker in execution?
- What is the use of param() method in jquery
- How to work with jQuery parent(), children() and siblings()?
- Difference between parent() and parents() in jQuery?
- What does jQuery data() function do?
- How do you check if an element exists or not in jQuery?
- How do I check if an HTML element is empty using jQuery?
- How to run an event handler only once in jQuery?
- How to Disable or Enable a Form Element Using jQuery
- Hide and show image on button click using jQuery
- Difference Between Prop and Attr in jQuery
- How do I check if an element is hidden in jQuery?
- Difference between return false; and e.preventDefault()
- What is each() function in jQuery? How do you use it?
- Which one is more efficient, document.getElementbyId( "myId") or $("#myId)?
- What is the difference between $.map and $.grep in jQuery
- What is the use of serialize method in jQuery
- What is the use of clone method in jQuery?
- What is event.PreventDefault in jQuery?
- Difference between event.PreventDefault and event.stopPropagation?
- What are source maps in jQuery?
- What does the jQuery migrate function do?
- Differences Between jQuery .bind() and .live()?
- How can you delay document.ready until a variable is set?
- How to disable cut,copy and paste in TextBox using jQuery?
- How to prevent Right Click option using jquery?
- How does the jQuery pushStack function work?
- Why use jQuery filter() Methods?
- Difference between find() and closest() in jquery?
- How To Use Ajax In Jquery?
- How to multiple AJAX requests be run simultaneously in jQuery?
- Can we call C# code behind using jQuery?
- How to include jQuery in ASP.Net project?
- Need to add jQuery file in both Master and Content page?
- Uncaught TypeError: $(…).modal is not a function jquery
- How to check whether a checkbox is checked in jQuery?
- Uncaught ReferenceError: $ is not defined
- How to Convert JSON Date to JavaScript/jQuery date