Promise Class
yui3/src/promise/js/promise.js:30
A promise represents a value that may not yet be available. Promises allow you to chain asynchronous operations, write synchronous looking code and handle errors throughout the process.
This constructor takes a function as a parameter where you can insert the logic that fulfills or rejects this promise. The fulfillment value and the rejection reason can be any JavaScript value. It's encouraged that rejection reasons be error objects
<pre><code> var fulfilled = new Y.Promise(function (resolve) { resolve('I am a fulfilled promise'); });
var rejected = new Y.Promise(function (resolve, reject) { reject(new Error('I am a rejected promise')); }); </code></pre>
Constructor
Promise
-
fn
Parameters:
-
fn
FunctionA function where to insert the logic that resolves this promise. Receives
resolve
andreject
functions as parameters. This function is called synchronously.
Index
Methods
Properties
Methods
_wrap
-
resolve
-
reject
-
fn
Wraps the callback in another function to catch exceptions and turn them into rejections.
Parameters:
-
resolve
FunctionResolving function of the resolver that handles this promise
-
reject
FunctionRejection function of the resolver that handles this promise
-
fn
FunctionCallback to wrap
Returns:
all
-
values
Returns a promise that is resolved or rejected when all values are resolved or any is rejected. This is useful for waiting for the resolution of multiple promises, such as reading multiple files in Node.js or making multiple XHR requests in the browser.
Parameters:
-
values
AnyAn array of any kind of values, promises or not. If a value is not
Returns:
[Promise] A promise for an array of all the fulfillment values
catch
-
[Function]
A shorthand for promise.then(undefined, callback)
.
Returns a new promise and the error callback gets the same treatment as in
then
: errors get caught and turned into rejections, and the return value
of the callback becomes the fulfilled value of the returned promise.
Parameters:
-
[Function]
Object optionalerrback Callback to be called in case this promise is rejected
Returns:
A new promise modified by the behavior of the error callback
getStatus
()
String
deprecated
Returns the current status of the operation. Possible results are "pending", "fulfilled", and "rejected".
Returns:
isPromise
-
obj
Checks if an object or value is a promise. This is cross-implementation compatible, so promises returned from other libraries or native components that are compatible with the Promises A+ spec should be recognized by this method.
Parameters:
-
obj
AnyThe object to test
Returns:
Whether the object is a promise or not
race
-
values
Returns a promise that is resolved or rejected when any of values is either resolved or rejected. Can be used for providing early feedback in the UI while other operations are still pending.
Parameters:
-
values
AnyAn array of values or promises
Returns:
reject
-
reason
A shorthand for creating a rejected promise.
Parameters:
-
reason
AnyReason for the rejection of this promise. Usually an Error Object
Returns:
A rejected promise
resolve
-
Any
Ensures that a certain value is a promise. If it is not a promise, it wraps it in one.
This method can be copied or inherited in subclasses. In that case it will
check that the value passed to it is an instance of the correct class.
This means that PromiseSubclass.resolve()
will always return instances of
PromiseSubclass
.
Parameters:
-
Any
Anyobject that may or may not be a promise
Returns:
then
-
[callback]
-
[errback]
Schedule execution of a callback to either or both of "fulfill" and
"reject" resolutions for this promise. The callbacks are wrapped in a new
promise and that promise is returned. This allows operation chaining ala
functionA().then(functionB).then(functionC)
where functionA
returns
a promise, and functionB
and functionC
may return promises.
Asynchronicity of the callbacks is guaranteed.
Parameters:
-
[callback]
Function optionalfunction to execute if the promise resolves successfully
-
[errback]
Function optionalfunction to execute if the promise resolves unsuccessfully
Returns:
A promise wrapping the resolution of either "resolve" or "reject" callback
Properties
_resolver
Object
private
A reference to the resolver object that handles this promise