This .promise() method in JQuery Returns a Promise object to be observed when certain type actions bounded to the collection, queued or not, are ended.
Syntax:
.promise([type][, target])
- type: This parameter specifies the type of queue which needed to be observed.
- target: This parameter specifies Object onto which the promise methods need to be attached.
Parameters:
Return Value: This method returns a dynamically generated Promise which is resolved once actions bounded to the collection, queued or not, have finished.
There are two examples discussed below:
- Example: In this example, the Deferred() is used to create a new object and after that then() method is called with notify and resolve method.
<!DOCTYPE HTML><html><head><title>JQuery.when() method</title></script></head><bodystyle="text-align:center;"><h1style="color:green">GeeksForGeeks</h1><pid="GFG_UP"></p><buttononclick="Geeks();">click here</button><pid="GFG_DOWN"></p><script>var el_up = document.getElementById("GFG_UP");el_up.innerHTML = "JQuery.when() method";var def = $.Deferred();function Geeks() {$.when().then(function(a) {alert( "when() method called this alert()." );});}</script></body></html>chevron_rightfilter_noneOutput:
Before clicking on button:

After clicking on button:

- Example: In this example, the Deferred() method is used and the state of Deferred object is checked.
<!DOCTYPE HTML><html><head><title>JQuery.when() method</title></script></head><bodystyle="text-align:center;"><h1style="color:green">GeeksForGeeks</h1><pid="GFG_UP"></p><buttononclick="Geeks();">click here</button><pid="GFG_DOWN"></p><script>var el_up = document.getElementById("GFG_UP");el_up.innerHTML = "JQuery.when() method";var def = $.Deferred();function Geeks() {$.when(def).done(function (x) {$('#GFG_DOWN').append('when() method is executed.')});def.resolve();}</script></body></html>chevron_rightfilter_noneOutput:

- JQuery deferred.notify() method
- JQuery deferred.notifyWith() method
- JQuery deferred.resolve() method
- JQuery deferred.state() method
- JQuery deferred.resolveWith() method
- JQuery deferred.then() method
- jQuery deferred.always() Method
- JQuery .Deferred() method
- jQuery deferred.rejectWith() Method
- jQuery deferred.catch() Method
- jQuery deferred.reject() Method
- jQuery deferred.fail() Method
- jQuery deferred.progress() Method
- JQuery deferred.pipe() Method
- jQuery deferred.done() Method
- JavaScript | promise resolve() Method
- JavaScript | Promise.all() Method
- How to wrap setTimeout() method in a promise ?
- JavaScript | Promise.allSettled() Method
- Node.js | fs.promise.readdir() Method
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


