Promise Class
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 (fulfill) {
fulfill('I am a fulfilled promise');
});
var rejected = new Y.Promise(function (fulfill, reject) {
reject(new Error('I am a rejected promise'));
});
Constructor
Promise
-
fn
Parameters:
-
fn
FunctionA function where to insert the logic that resolves this promise. Receives
fulfill
andreject
functions as parameters. This function is called synchronously.
Methods
getStatus
()
String
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:
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.