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
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'));
});
Promise
fn
fn
Function
A function where to insert the logic that resolves this
promise. Receives resolve
and reject
functions as parameters.
This function is called synchronously.
_wrap
resolve
reject
fn
Wraps the callback in another function to catch exceptions and turn them into rejections.
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.
values
Any
An array of any kind of values, promises or not. If a value is not
[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.
[Function]
Object
optional
errback Callback to be called in case this promise is rejected
A new promise modified by the behavior of the error callback
getStatus
Returns the current status of the operation. Possible results are "pending", "fulfilled", and "rejected".
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.
obj
Any
The object to test
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.
values
Any
An array of values or promises
reject
reason
A shorthand for creating a rejected promise.
reason
Any
Reason for the rejection of this promise. Usually an Error Object
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
.
Any
Any
object that may or may not be a promise
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.
A promise wrapping the resolution of either "resolve" or "reject" callback