deferred.promise( [target ] ) Returns: Promise
Description: Return a Deferred's Promise object.
-
version added: 1.5deferred.promise( [target ] )
-
targetType: ObjectObject onto which the promise methods have to be attached
-
The deferred.promise()
method allows an asynchronous function to prevent other code from interfering with the progress or status of its internal request. The Promise exposes only the Deferred methods needed to attach additional handlers or determine the state (then
, done
, fail
, always
, pipe
, progress
, and state
), but not ones that change the state (resolve
, reject
, notify
, resolveWith
, rejectWith
, and notifyWith
).
If target
is provided, deferred.promise()
will attach the methods onto it and then return this object rather than create a new one. This can be useful to attach the Promise behavior to an object that already exists.
If you are creating a Deferred, keep a reference to the Deferred so that it can be resolved or rejected at some point. Return only the Promise object via deferred.promise()
so other code can register callbacks or inspect the current state.
For more information, see the documentation for Deferred object.
Examples:
Example: Create a Deferred and set two timer-based functions to either resolve or reject the Deferred after a random interval. Whichever one fires first "wins" and will call one of the callbacks. The second timeout has no effect since the Deferred is already complete (in a resolved or rejected state) from the first timeout action. Also set a timer-based progress notification function, and call a progress handler that adds "working..." to the document body.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
|
Example: Use the target argument to promote an existing object to a Promise:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
|