All Downloads are FREE. Search and download functionalities are using the official Maven repository.

elemental2.promise.Promise Maven / Gradle / Ivy

/*
 * 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 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);
    }
  }

  public static native  Promise all(IThenable... promises);

  public static native  Promise race(IThenable... 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 onRejected);

  @Override
  public native  Promise then(
      ThenOnFulfilledCallbackFn onFulfilled,
      ThenOnRejectedCallbackFn onRejected);

  @Override
  public native  Promise then(ThenOnFulfilledCallbackFn onFulfilled);
}