elemental2.promise.Promise Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2018 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
/*
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*
*/
package elemental2.promise;
import jsinterop.annotations.JsFunction;
import jsinterop.annotations.JsMethod;
import jsinterop.annotations.JsOverlay;
import jsinterop.annotations.JsPackage;
import jsinterop.annotations.JsType;
import jsinterop.base.Js;
/**
* The Promise object is used for asynchronous computations. A Promise represents a value which may
* be available now, or in the future, or never.
*
* @see Promise
*/
@JsType(isNative = true, namespace = JsPackage.GLOBAL)
public class Promise implements IThenable {
@JsFunction
public interface FinallyOnFinallyCallbackFn {
void onInvoke();
}
@JsFunction
public interface CatchOnRejectedCallbackFn {
IThenable onInvoke(Object error);
}
@JsFunction
public interface PromiseExecutorCallbackFn {
@JsFunction
interface RejectCallbackFn {
void onInvoke(Object error);
}
@JsFunction
interface ResolveCallbackFn {
@JsType(isNative = true, name = "?", namespace = JsPackage.GLOBAL)
interface ResolveUnionType {
@JsOverlay
static ResolveUnionType> of(Object o) {
return Js.cast(o);
}
@JsOverlay
default IThenable asIThenable() {
return Js.cast(this);
}
@JsOverlay
default T asT() {
return Js.cast(this);
}
}
@JsOverlay
default void onInvoke(IThenable value) {
onInvoke(Js.>uncheckedCast(value));
}
void onInvoke(ResolveUnionType p0);
@JsOverlay
default void onInvoke(T value) {
onInvoke(Js.>uncheckedCast(value));
}
}
void onInvoke(ResolveCallbackFn resolve, RejectCallbackFn reject);
}
@JsType(isNative = true, name = "?", namespace = JsPackage.GLOBAL)
public interface ResolveValueUnionType {
@JsOverlay
static ResolveValueUnionType> of(Object o) {
return Js.cast(o);
}
@JsOverlay
default IThenable asIThenable() {
return Js.cast(this);
}
@JsOverlay
default V asV() {
return Js.cast(this);
}
}
@JsOverlay
public static Promise all(IThenable extends V>... promises) {
return allInternal(promises);
}
@JsMethod(name = "all")
private static native Promise allInternal(IThenable extends V>[] promises);
@JsOverlay
public static Promise race(IThenable extends V>... promises) {
return raceInternal(promises);
}
@JsMethod(name = "race")
private static native Promise raceInternal(IThenable extends V>[] promises);
public static native Promise reject(Object error);
@JsOverlay
public static final Promise resolve(IThenable value) {
return resolve(Js.>uncheckedCast(value));
}
public static native Promise resolve(ResolveValueUnionType value);
@JsOverlay
public static final Promise resolve(V value) {
return resolve(Js.>uncheckedCast(value));
}
public Promise(PromiseExecutorCallbackFn executor) {}
@JsMethod(name = "catch")
public native Promise catch_(CatchOnRejectedCallbackFn extends V> onRejected);
@Override
public native Promise then(
ThenOnFulfilledCallbackFn super T, ? extends V> onFulfilled,
ThenOnRejectedCallbackFn extends V> onRejected);
@Override
public native Promise then(ThenOnFulfilledCallbackFn super T, ? extends V> onFulfilled);
@JsMethod(name = "finally")
public native Promise finally_(FinallyOnFinallyCallbackFn onFinally);
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy