The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

Here is the example Scala code, it assumes a function called fetch which when given a URL will return a Future.

  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.

  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.

  val f = fetch(url) rescue {
    case ConnectionFailed =>
      fetch(url)
  }