Return Twitter's Future within a certain duration

121 views Asked by At

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?

1

There are 1 answers

1
Zafiro Geta On BEST ANSWER

In your case you should use Await.result instead.

Example:

Await.result(future, Duration#fromSeconds);