Interface JsPromise

All Superinterfaces:
AutoCloseable, Closeable, JsObject

public interface JsPromise extends JsObject, Closeable
A JavaScript Promise.

Allows using Java code as promise handlers. The Promise object is alive while the web page where it was created is loaded. When the web page is unloaded, all the JavaScript objects are disposed. An attempt to access the already disposed JavaScript Promise will lead to the IllegalStateException.

Usage example:

   
     JsPromise jsPromise = frame.executeJavaScript("Promise.resolve('Resolved')");
     jsPromise.then(results -> System.out.println(results[0]));
   
 
  • Method Details

    • then

      JsPromise then(JsPromiseHandler onFulfilled, JsPromiseHandler onRejected)
      Registers fulfillment and rejection handlers for this promise and returns a new one.

      The appropriate handler is called immediately if the promise is already settled.

      Parameters:
      onFulfilled - a handler that is invoked if the promise is fulfilled
      onRejected - a handler that is invoked if the promise is rejected
      Throws:
      ObjectClosedException - if the JavaScript promise is already disposed
      See Also:
    • then

      JsPromise then(JsPromiseHandler onFulfilled)
      Registers a fulfillment handler for this promise and returns a new one.

      The handler is called immediately if the promise is already fulfilled.

      Parameters:
      onFulfilled - a handler that is invoked if the promise is fulfilled
      Throws:
      ObjectClosedException - if the JavaScript promise is already disposed
      See Also:
    • catchError

      JsPromise catchError(JsPromiseHandler onRejected)
      Registers a rejection handler for this promise and returns a new one.

      The handler is called immediately if the promise is already rejected.

      Parameters:
      onRejected - a handler that is invoked if the promise is rejected
      Throws:
      ObjectClosedException - if the JavaScript promise is already disposed
      See Also:
    • finallyExecute

      JsPromise finallyExecute(JsPromiseHandler onFinally)
      Registers a handler that will be invoked if the promise is settled (fulfilled/rejected) and returns a new promise.

      The handler is called immediately if the promise is already settled.

      Parameters:
      onFinally - a handler that is invoked if the promise is settled
      Throws:
      ObjectClosedException - if the JavaScript promise is already disposed
      See Also: