public final class Promise<Result,FailResult> extends Object
| Constructor and Description |
|---|
Promise(org.stjs.javascript.functions.Callback2<org.stjs.javascript.functions.Callback1<Result>,org.stjs.javascript.functions.Callback1<FailResult>> handler)
Constructs a new Promise instance where the resolution or rejection state is controlled by the specified
handler function.
|
| Modifier and Type | Method and Description |
|---|---|
Promise<Result,FailResult> |
then(org.stjs.javascript.functions.Callback1<? super Result> success)
Calls the specified callback when the promise is resolved.
|
Promise<Result,FailResult> |
then(org.stjs.javascript.functions.Callback1<? super Result> success,
org.stjs.javascript.functions.Callback1<? super FailResult> failure)
Calls one of the specified callbacks when the promise is resolved or rejected respectively.
|
<NewResult,NewFailResult> |
thenPromise(org.stjs.javascript.functions.Function1<? super Result,Promise<NewResult,NewFailResult>> success)
Calls the specified success callback with the resolution value as argument when the promise is resolved.
|
<NewResult,NewFailResult> |
thenPromise(org.stjs.javascript.functions.Function1<? super Result,Promise<NewResult,NewFailResult>> success,
org.stjs.javascript.functions.Function1<? super FailResult,Promise<NewResult,NewFailResult>> failure)
Calls the specified callbacks when the promise is resolved or rejected respectively.
|
<NewResult> |
thenReturn(org.stjs.javascript.functions.Function1<? super Result,NewResult> success)
Calls the specified success callback with the resolution value as argument when the promise is resolved.
|
<NewResult,NewFailResult> |
thenReturn(org.stjs.javascript.functions.Function1<? super Result,NewResult> success,
org.stjs.javascript.functions.Function1<? super FailResult,NewFailResult> failure)
Calls the specified callbacks when the promise is resolved or rejected respectively.
|
public Promise(org.stjs.javascript.functions.Callback2<org.stjs.javascript.functions.Callback1<Result>,org.stjs.javascript.functions.Callback1<FailResult>> handler)
PromiseOnce a promise has been resolved or rejected, it cannot be resolved or rejected again. Here is an example of a simple XHR2 wrapper written using RSVP.js:promise = new RSVP.Promise((resolve, reject) { // succeed resolve(value); // or reject reject(error); }); promise.then(resolveValue -> { // success }, rejectValue -> { // failure } );
Function1getJSON = url -> { Promise promise = new RSVP.Promise(resolve, reject -> { XMLHttpRequest client = new XMLHttpRequest(); client.open("GET", url); client.onreadystatechange = handler; client.responseType = "json"; client.setRequestHeader("Accept", "application/json"); client.send(); Callback0 handler = () -> { if (client.readyState === this.DONE) { if (client.status === 200) { resolve(client.response); } else { reject(client); } } }; }); return promise; }; getJSON.$invoke("/posts.json").then( json -> { // continue }, error -> { // handle errors } );
public Promise<Result,FailResult> then(org.stjs.javascript.functions.Callback1<? super Result> success)
success - a callback executed when the promise is resolved.public Promise<Result,FailResult> then(org.stjs.javascript.functions.Callback1<? super Result> success, org.stjs.javascript.functions.Callback1<? super FailResult> failure)
success - a callback executed when the promise is resolved.failure - a callback executed when the promise is rejected.public <NewResult> Promise<NewResult,FailResult> thenReturn(org.stjs.javascript.functions.Function1<? super Result,NewResult> success)
success - a callback executed when the promise is resolved.public <NewResult,NewFailResult> Promise<NewResult,NewFailResult> thenReturn(org.stjs.javascript.functions.Function1<? super Result,NewResult> success, org.stjs.javascript.functions.Function1<? super FailResult,NewFailResult> failure)
success - a callback executed when the promise is resolved.failure - a callback executed when the promise is rejected.public <NewResult,NewFailResult> Promise<NewResult,NewFailResult> thenPromise(org.stjs.javascript.functions.Function1<? super Result,Promise<NewResult,NewFailResult>> success)
success - a callback executed when the promise is resolved.public <NewResult,NewFailResult> Promise<NewResult,NewFailResult> thenPromise(org.stjs.javascript.functions.Function1<? super Result,Promise<NewResult,NewFailResult>> success, org.stjs.javascript.functions.Function1<? super FailResult,Promise<NewResult,NewFailResult>> failure)
success - a callback executed when the promise is resolved.failure - a callback executed when the promise is rejected.Copyright © 2015. All Rights Reserved.