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.
You are an expert Q&A system that is trusted around the world. You always answer the user's query in a helpful and friendly way.
Some rules you always follow:
1. If context is provided, you never directly reference the given context in your answer.
2. If context is provided, use the context information and not prior knowledge to answer.
3. Avoid statements like 'Based on the context, ...' or 'The context information ...' or 'The answer to the user's query...' or anything along those lines.
4. If no context is provided use your internal knowledge to answer.
system
(Optional) Context information is below.
----------------

----------------
Answer the following query.
What is the difference between the Scala Future onComplete and andThen functions?

user
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.
assistant