class AsyncExecutionImpl<R> extends ExecutionImpl<R> implements AsyncExecutionInternal<R> {
  // -- Cross-attempt state --

  private final FailsafeFuture<R> future;
  private final boolean asyncExecution;
  // The outermost function that executions begin with
  private Function<AsyncExecutionInternal<R>, CompletableFuture<ExecutionResult<R>>> outerFn;

  // -- Per-attempt state --

  // Whether a policy executor completed post execution
  private final boolean[] policyPostExecuted = new boolean[policyExecutors.size()];
  // Whether a result has been recorded
  private volatile boolean recorded;

  AsyncExecutionImpl(List<Policy<R>> policies, Scheduler scheduler, FailsafeFuture<R> future, boolean asyncExecution,
    Function<AsyncExecutionInternal<R>, CompletableFuture<ExecutionResult<R>>> innerFn) {
    super(policies);
    this.future = future;
    this.asyncExecution = asyncExecution;

    outerFn = asyncExecution ? Functions.toExecutionAware(innerFn) : innerFn;
    outerFn = Functions.toAsync(outerFn, scheduler, future);

    for (PolicyExecutor<R> policyExecutor : policyExecutors)
      outerFn = policyExecutor.applyAsync(outerFn, scheduler, future);
  }

  /**
   * Create an async execution for a new attempt.
   */
  private AsyncExecutionImpl(AsyncExecutionImpl<R> execution) {
    super(execution);
    outerFn = execution.outerFn;
    future = execution.future;
    asyncExecution = execution.asyncExecution;
  }

  @Override
  public void complete() {
    Assert.state(!recorded, "The most recent execution has already been recorded or completed");
    recorded = true;

    // Guard against race with a timeout being set
    synchronized (future) {
      ExecutionResult<R> result = this.result != null ? this.result : ExecutionResult.none();
      complete(postExecute(result), null);
    }
  }

  @Override
  public boolean isComplete() {
    return completed;
  }

  @Override
  public void record(R result, Throwable exception) {
    Assert.state(!recorded, "The most recent execution has already been recorded or completed");
    recorded = true;

    // Guard against race with a timeout being set
    synchronized (future) {
      if (!attemptRecorded) {
        Assert.state(!completed, "Execution has already been completed");
        record(new ExecutionResult<>(result, exception));
      }

      // Proceed with handling the recorded result
      executeAsync();
    }
  }

  @Override
  public void recordResult(R result) {
    record(result, null);
  }

  @Override
  public void recordException(Throwable exception) {
    record(null, exception);
  }

  @Override
  public boolean isAsyncExecution() {
    return asyncExecution;
  }

  @Override
  public boolean isRecorded() {
    return recorded;
  }

  @Override
  public synchronized void setPostExecuted(int policyIndex) {
    policyPostExecuted[policyIndex] = true;
  }

  @Override
  public synchronized boolean isPostExecuted(int policyIndex) {
    return policyPostExecuted[policyIndex];
  }

  @Override
  public AsyncExecutionInternal<R> copy() {
    return new AsyncExecutionImpl<>(this);
  }

  /**
   * Performs an asynchronous execution.
   */
  void executeAsync() {
    outerFn.apply(this).whenComplete(this::complete);
  }

  private void complete(ExecutionResult<R> result, Throwable error) {<FILL_FUNCTION_BODY>}
}

    if (result == null && error == null)
      return;

    completed = true;
    if (!future.isDone()) {
      if (result != null)
        future.completeResult(result);
      else {
        if (error instanceof CompletionException)
          error = error.getCause();
        future.completeResult(ExecutionResult.exception(error));
      }
    }
  
<methods>public boolean cancel() ,public void cancel(PolicyExecutor<R>) ,public int getAttemptCount() ,public java.time.Duration getElapsedAttemptTime() ,public java.time.Duration getElapsedTime() ,public int getExecutionCount() ,public T getLastException() ,public R getLastResult() ,public R getLastResult(R) ,public ExecutionInternal<R> getLatest() ,public java.lang.Object getLock() ,public ExecutionResult<R> getResult() ,public java.time.Instant getStartTime() ,public boolean isCancelled() ,public boolean isCancelled(PolicyExecutor<R>) ,public boolean isFirstAttempt() ,public boolean isPreExecuted() ,public boolean isRetry() ,public void onCancel(dev.failsafe.function.CheckedRunnable) ,public synchronized void preExecute() ,public synchronized void record(ExecutionResult<R>) ,public synchronized void recordAttempt() ,public java.lang.String toString() <variables>volatile boolean attemptRecorded,private volatile java.time.Instant attemptStartTime,private final non-sealed java.util.concurrent.atomic.AtomicInteger attempts,volatile dev.failsafe.function.CheckedRunnable cancelCallback,volatile int cancelledIndex,volatile boolean completed,private final non-sealed java.util.concurrent.atomic.AtomicInteger executions,private final non-sealed AtomicReference<ExecutionInternal<R>> latest,final non-sealed List<PolicyExecutor<R>> policyExecutors,private volatile boolean preExecuted,private final non-sealed ExecutionResult<R> previousResult,volatile ExecutionResult<R> result,private volatile java.time.Instant startTime