private void safeTransferToEdt(T result) {
      if (Promises.isRejected(this)) {
        cancelJob(null);
        return;
      }

      long stamp = AsyncExecutionServiceImpl.getWriteActionCounter();

      ApplicationManager.getApplication().invokeLater((ContextAwareRunnable) () -> {
        if (stamp != AsyncExecutionServiceImpl.getWriteActionCounter()) {
          reschedule();
          return;
        }

        if (checkObsolete()) {
          cancelJob(null);
          return;
        }

        setResult(result);

        if (isSucceeded()) { // in case when another thread managed to cancel it just before `setResult`
          try {
            if (AppExecutorUtil.propagateContext()) {
              try (AccessToken ignored = ThreadContext.installThreadContext(myChildContext.getContext(), false)) {
                builder.myUiThreadAction.accept(result);
              }
            } else {
              builder.myUiThreadAction.accept(result);
            }
          } finally {
            completeJob();
          }
        }
      }, builder.myModalityState, __ -> isCancelled());
    }
private void safeTransferToEdt(T result)