What is the difference between the Scala Future onComplete and andThen functions?
Both `andThen` and `onComplete` allow you to observe the result of some future via a callback function. However, `andThen` can be chained, and unlike `onComplete`, it guarantees the order in which callback functions will be applied. In addition, the callback given to `andThen` may be a partial function, handling only a subset of outcomes (e.g., only `Success` or only `Failure`).

Consider the following example using `andThen` and `map`:

```
def demoAndThen(fut: Future[Int]): Future[Int] = {
  fut.andThen { case Success(value) => println(s"andThen1: $value") }
    .andThen { case Success(value) => println(s"andThen2: $value") }
    .map { i =>
      println(s"map: $i")
      i
    }
}
```

When `fut` completes successfully, "andthen1: ...", "andThen2: ..." and "map: ..." will be printed in that order.

Consider the analogous example using `onComplete` and `map`:

```
def demoOnComplete(fut: Future[Int]): Future[Int] = {
  fut.onComplete { t: Try[Int] => println(s"onComplete1: $t") }
  fut.onComplete { t: Try[Int] => println(s"onComplete2: $t") }
  fut.map { i =>
    println(s"map: $i")
    i
  }
}
```

In this case, when `fut` completes successfully, "onComplete1: ...", "onComplete2: ..." and "map: ..." will be printed in some unspecified order.

Although `andThen` is chained, the callback function does not generally affect the outcome of the resulting `Future`. But be aware: if the callback throws some fatal errors, the resulting future will never complete.
Qual é a diferença entre as funções Scala Future onComplete e andThen?
No entanto, o `andThen` e o `onComplete` permitem observar o resultado de um futuro através de uma função de chamada de volta. No entanto, o `andThen` pode ser encadenado e, ao contrário do `onComplete`, garante a ordem em que as funções de chamada de volta serão aplicadas. Além disso, a chamada de volta dada ao `andThen` pode ser uma função parcial, lidando apenas com um subconjunto de resultados (por exemplo, apenas `Success errors` ou apenas `Failure`). Considere o exemplo seguinte usando o `andThen` e o `map2: `andThen`: def def: Future: [Int]1: {In: ``final: def: Future: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: int: [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] No: [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] [] No: [] No: [] No: [] No: [] No: [] Se: [] Se: [] Se: [] Se: [] Se: [] Se: [] Se: [] Se: [] Se: [**********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************