Skip to content

Return Twitter’s Future within a certain duration

According to Twitter Util’s Future documentation, Future.within() or .by() “Returns a new Future that fails if it is not satisfied before the given time”.

I was trying to use this, in Java, to return a (Future) String if getDownload() takes less than 5 secs, and return / throw an exception if getDownload() takes more than 5 seconds.

Future<String> futureDownload = getDownload();
return futureDownload.within(DefaultTimer.twitter(), Duration.fromSeconds(5));

I cannot get it to work. If getDownload() takes, for example, 15 secs, it returns after 15 secs.

What am I missing?

Answer

In your case you should use Await.result instead.

Example:

Await.result(future, Duration#fromSeconds);