Here is the example Scala code, it assumes a function called fetch which when given a URL will return a Future.
fetch
def getThumbnail(url: String): Future[Webpage] = { val promise = new Promise[Webpage] fetch(url) onSuccess { page => fetch(page.imageLinks(0)) onSuccess { p => promise.setValue(p) } onFailure { exc => promise.setException(exc) } } onFailure { exc => promise.setException(exc) } promise }
Scala Futures have a method called flatMap, which takes a function that given value will return another Future.
flatMap
def getThumbnail(url: String): Future[Webpage] = fetch(url) flatMap { page => fetch(page.imageLinks(0)) }
Scala Futures also have a rescue method which can serve as a kind of catch block that potentially will return another Future.
rescue
val f = fetch(url) rescue { case ConnectionFailed => fetch(url) }
To install Promises, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Promises
CPAN shell
perl -MCPAN -e shell install Promises
For more information on module installation, please visit the detailed CPAN module installation guide.