Class ScriptOperation

java.lang.Object
com.deltawalker.script.ScriptOperation

public final class ScriptOperation extends Object
A long-running operation - compare, merge, sync, report or print - that scripts await.

Methods that start such an operation return immediately; the work runs in the background. Comparison.compare() and Comparison.print() return a native JavaScript Promise; merge(), sync() and report() return a ScriptOperation. Both are thenables and are used the same way:


 // Sequential: the report starts only after the comparison has finished
 await comp.compare();
 await comp.report(reportPath, ReportKind.HTML, null);

 // Parallel: both comparisons run at the same time
 await Promise.all([compA.compare(), compB.compare()]);

 // Start now, await later
 const op = comp.compare();
 console.log('Comparison running...');
 await op;
 

Failure and cancellation

An operation that fails rejects with a string describing the failure, for instance when another operation is already pending on the same comparison. An operation that is cancelled, for instance because its comparison was closed while it was pending, rejects with a ScriptCancellationError whose cancelled property is true:


 try {
     await comp.compare();
 } catch (e) {
     if (e.cancelled) {
         console.log('Cancelled: ' + e.message);
     } else {
         console.error('Failed: ' + e);
     }
 }
 

If no pending operation completes within the script timeout - 5 minutes by default, configurable with the JVM option -Ddw.script.waitTimeoutMs=<ms> - the script itself is stopped with a timeout error; this cannot be caught by the script.

Await a ScriptOperation returned by merge(), sync() or report() only once. To await it several times, or to pass it to Promise.all() more than once, wrap it first: const p = Promise.resolve(comp.report(...));.

Since:
3.0
  • Method Details

    • isResolved

      public boolean isResolved()
      Answers whether this operation has completed successfully.
      Returns:
      true if the operation has succeeded, false if it is still pending, has failed or has been cancelled
    • isCancelled

      public boolean isCancelled()
      Answers whether this operation has been cancelled.
      Returns:
      true if the operation has been cancelled, false otherwise
    • rejectionReason

      public String rejectionReason()
      Returns why this operation failed or was cancelled.
      Returns:
      the failure or cancellation message, or null if the operation is pending or has succeeded
    • then

      public Object then(Object onFulfilled, Object onRejected)
      The JavaScript thenable protocol: registers the functions to call when this operation settles. The JavaScript engine calls this method when a script awaits the operation or passes it to Promise.resolve() or Promise.all(); scripts rarely need to call it directly.

      onFulfilled is called without arguments when the operation succeeds. onRejected is called with a failure message, or with a ScriptCancellationError when the operation is cancelled. Only the functions passed to the most recent call are kept.

      Parameters:
      onFulfilled - the function to call when the operation succeeds
      onRejected - the function to call when the operation fails or is cancelled
      Returns:
      always null; unlike Promise.then(), this method does not return a new promise