Class Promise<RESULT>

  • Type Parameters:
    RESULT - This type indicates the type used by return-value that's ultimately received by the user. This type is the class which is actually returned when a user makes a call to Promise.await().
    All Implemented Interfaces:
    java.util.concurrent.CompletionStage<RESULT>, java.util.concurrent.Future<RESULT>

    public class Promise<RESULT>
    extends java.util.concurrent.CompletableFuture<RESULT>
    Primarily a wrapper around Java's CompletableFuture that provides two or three simplified methods for awaiting a promise.

    The three classes, Script, Promise and Sender are designed to allow for the creation of 'Asynchronous Scripts' that may be executed and made to appear to be synchronous through the Promise.await() method.

    These three classes are made use of in the Browser Remote Debugging Protocol Package. Theoretically, they could be used by any asynchronous communication system.


    • Nested Class Summary

      • Nested classes/interfaces inherited from class java.util.concurrent.CompletableFuture

        java.util.concurrent.CompletableFuture.AsynchronousCompletionTask
    • Field Summary

       
      The Script which has made this Promise
      Modifier and Type Field
      Script<RESULT> script
       
      The JsonObject that has produced the RESULT
      Modifier and Type Field
      JsonObject jo
    • Method Summary

       
      Waits for this Promise to resolve, and return a RESULT, or throw.
      Modifier and Type Method
      RESULT await()
      RESULT await​(long timeout, TimeUnit unit)
      • Methods inherited from class java.util.concurrent.CompletableFuture

        acceptEither, acceptEitherAsync, acceptEitherAsync, allOf, anyOf, applyToEither, applyToEitherAsync, applyToEitherAsync, cancel, complete, completeAsync, completeAsync, completedFuture, completedStage, completeExceptionally, completeOnTimeout, copy, defaultExecutor, delayedExecutor, delayedExecutor, exceptionally, failedFuture, failedStage, get, get, getNow, getNumberOfDependents, handle, handleAsync, handleAsync, isCancelled, isCompletedExceptionally, isDone, join, minimalCompletionStage, newIncompleteFuture, obtrudeException, obtrudeValue, orTimeout, runAfterBoth, runAfterBothAsync, runAfterBothAsync, runAfterEither, runAfterEitherAsync, runAfterEitherAsync, runAsync, runAsync, supplyAsync, supplyAsync, thenAccept, thenAcceptAsync, thenAcceptAsync, thenAcceptBoth, thenAcceptBothAsync, thenAcceptBothAsync, thenApply, thenApplyAsync, thenApplyAsync, thenCombine, thenCombineAsync, thenCombineAsync, thenCompose, thenComposeAsync, thenComposeAsync, thenRun, thenRunAsync, thenRunAsync, toCompletableFuture, toString, whenComplete, whenCompleteAsync, whenCompleteAsync
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • jo

        🡅  🡇     🗕  🗗  🗖
        public volatile JsonObject jo
        This field will hold the json response which has been recived by the WebSocket's layer. This field is assigned immediately preceeding this future's completion. The json stored here will only be the "result" property of the surrounding datagram json envelope.

        Because CDP may or may not be a "moving target", this field is provided as a manner of conenience to the developer's wanting to see how their response POJO's were built. In normal operations, there is likely little use for inspecting the raw protocol json.

        This field is updated by the response handler's immediately prior to invoking the CompletableFuture.complete method. It is initialized to null. There are also logs which contain the json that has been sent and received over the WebSocket's layer, although they are less convenient to use.
        Code:
        Exact Field Declaration Expression:
         public volatile JsonObject jo = null;
        
    • Method Detail

      • await

        🡅  🡇     🗕  🗗  🗖
        public RESULT await()
        Nearly identical to the CompletableFuture method join.

        This method is nothing more than a wrapper around the CompletableFuture method known as join(long). This does a little bit of Exception Unwrapping, as may be viewed in the Hi-Lited Code, below.
        Returns:
        the long-lost, long-awaited-for, result.
        Code:
        Exact Method Body:
         try 
             { return this.join(); }
        
         catch (CompletionException e)
         {
             Throwable c = e.getCause();
             if (c == null) throw e;
             if (c instanceof RDPException)          throw (RDPException) c;
             if (c instanceof BrowserException)      throw (BrowserException) c;
             if (c instanceof BrowserErrorException) throw (BrowserErrorException) c;
             if (c instanceof AsynchronousException) throw (AsynchronousException) c;
             throw e;
         }
        
      • await

        🡅     🗕  🗗  🗖
        public RESULT await​(long timeout,
                            java.util.concurrent.TimeUnit unit)
        Waits if necessary for at most the given time for this future to complete, and then returns its result, if available.

        This method is nothing more than a wrapper around the CompletableFuture method known as get(long, TimeUnit). This method simply converts a slew of Checked-Exceptions into Unchecked / RuntimeExceptions. No more, no less.
        Parameters:
        timeout - The maximum time to wait
        unit - the time unit of the timeout parameter
        Returns:
        the result value
        Throws:
        UncheckedExecutionException - if this future completed exceptionally
        UncheckedInterruptedException - if the current thread was interrupted while waiting
        UncheckedTimeoutException - if the waiting timed out
        java.util.concurrent.CompletionException - if this future was cancelled
        RDPException
        BrowserException
        BrowserErrorException
        AsynchronousException
        Code:
        Exact Method Body:
         try 
             { return this.get(timeout, unit); }
        
         catch (ExecutionException e)
             { throw new UncheckedExecutionException(e); }
        
         catch (InterruptedException e)
             { throw new UncheckedInterruptedException(e); }
        
         catch (TimeoutException e)
             { throw new UncheckedTimeoutException(e); }
        
         catch (CompletionException e)
         {
             Throwable c = e.getCause();
             if (c == null) throw e;
             if (c instanceof RDPException)          throw (RDPException) c;
             if (c instanceof BrowserException)      throw (BrowserException) c;
             if (c instanceof BrowserErrorException) throw (BrowserErrorException) c;
             if (c instanceof AsynchronousException) throw (AsynchronousException) c;
             throw e;
         }