Yes, code that returns the future (third-party APIs) will be immediately executed and return an unfinished future.
The fulfillment of this Future to the fullest has nothing to do with the actor who began to live it.
If you no longer need this actor, you do not need to wait for the completion of the "Future", and you can stop the actor, as you do in the first example.
If you need to do something in this actor, as a result of this future, you can set the onComplete callback for this future. Once the future is complete, he can send a message to the actor to stop. For example, for example:
val myActor = self
EDIT
Another alternative suggested in the comments is to use a pipeTo usage pipeTo . It is almost the same. Here's how it is implemented in the Akka library:
def pipeTo(recipient: ActorRef)(implicit sender: ActorRef = Actor.noSender): Future[T] = { future onComplete { case Success(r) β recipient ! r case Failure(f) β recipient ! Status.Failure(f) } future }
Here's how you can call it after creating the Future:
p pipeTo myActor
The main difference is that your actor will have to disconnect after receiving messages, and an error message is reported to the actor with the message Failure . This method is a little safer to use, because you need to pass an ActorRef , and you do not need to store it (self) in a variable.
yΗsΚΗlA
source share