Promise
snow.api.Promise
defined in ``
The Promise interface represents a proxy for a value not necessarily known when the promise is created. It allows you to associate handlers to an asynchronous action’s eventual success or failure. This lets asynchronous methods return values like synchronous methods: instead of the final value, the asynchronous method returns a promise of having a value at some point in the future.
A pending promise can become either fulfilled with a value, or rejected with a reason. When either of these happens, the associated handlers queued up by a promise’s then method are called. (If the promise has already been fulfilled or rejected when a corresponding handler is attached, the handler will be called, so there is no race condition between an asynchronous operation completing and its handlers being attached.)
As the Promise.prototype.then and Promise.prototype.error methods return promises, they can be chained—an operation called composition.
Documentation provided mostly by MDN licensed under CC-BY-SA 2.5. by Mozilla Contributors. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
class
Members
Methods
allstatic
all(list:Array<snow.api.Promise>) : snow.api.Promise
The Promise.all(iterable) function returns a promise that resolves when all of the promises in the iterable argument have resolved. The result is passed as an array of values from all the promises. If any of the passed in promises rejects, the all Promise immediately rejects with the value of the promise that rejected, discarding all the other promises whether or not they have resolved. error
error<T>(on_rejected:error.T) : snow.api.Promise
The error function returns a Promise and deals with rejected cases only. It behaves the same as calling then(null, on_rejected). new
new<T>(func:new.T) : Void
Creates a new promise by providing a function with two callback arguments. Inside this function, invoking these callbacks controls the promise state.
For example, if fetching a value async, and the operation fails, you would
invoke the second callback with the reason/error. If the operation succeeded,
you would invoke the first.
`new Promise(function(resolve, reject) {
var success = ... get value ...
if(success) {
resolve(value);
} else {
reject(Error(...));
}
});`</span>
new Promise(function(resolve, reject) {
var value = get_value();
if(value != null) {
resolve(value);
} else {
reject(Error(...));
}
});
racestatic
race(list:Array<snow.api.Promise>) : snow.api.Promise
The Promise.race function returns a promise that resolves or rejects as soon as one of the promises in the list resolves or rejects, with the value or reason from that promise. rejectstatic
reject<T>(reason:reject.T) : snow.api.Promise
The Promise.reject function returns a Promise object that is rejected with the optional reason. resolvestatic
resolve<T>(val:resolve.T) : snow.api.Promise
The static Promise.resolve function returns a Promise object that is resolved with the given value. then
then<T>, T1>(on_fulfilled:then.T, on_rejected:then.T) : snow.api.Promise
The then function returns a Promise. It takes two arguments, both are callback functions for the success and failure cases of the Promise.
var load = promise_returning_function();
load.then(function(value:Type) {
//use value
}).error(function(error:Error) {
//use error
});