spotify / completable-futures

Utilities for working with futures in Java 8
Apache License 2.0
393 stars 51 forks source link

Add additional helper methods to wrap CompletableFutures.successfulAsList #93

Closed KathrynN closed 11 months ago

KathrynN commented 1 year ago

This PR adds two helper functions to CompletableFutures

    CompletableFutures.successfulAsList(
        domainSpecificEntity.videoSource().stream()
            .map(
                video ->
                    originalFileLocationStore.insert(....))
            .toList(), // this bit
        e -> {
          LOG.error("failed to insert video for creator {}", domainSpecificEntity.spotifyUri(), e);
          return null;
        });
    var creatorsFuture =
        CompletableFutures.successfulAsList(
            creatorUris.stream().map(creatorDataStore::getCreator).collect(Collectors.toList()),
            t -> {
              if (t instanceof CreatorNotFoundException) {
                return CreatorMetadata.newBuilder()
                    .setSpotifyUri(((CreatorNotFoundException) t).getUri())
                    .build();
              }
              return null; // here because it's not a known exception I lose the creator's info.
            });
mbruggmann commented 1 year ago

The first case is quite well supported already using something like this, no?

values.stream()
    .map(value -> asyncOperation(value))
    .map(future -> future.exceptionally(...))
    .collect(joinList());
KathrynN commented 11 months ago

Spoken to both Kristofer and Marc, and we all three decided that this contribution didn't add enough value to be merged