I was wondering when exactly a Dart program terminates.
Take the following program:
import 'dart:isolate';
Future<void> calculateTheAnswer(SendPort port) async {
await Future.delayed(Duration(seconds: 1));
port.send(42);
}
void main() async {
var port = ReceivePort()..listen(print);
await Isolate.spawn(calculateTheAnswer, port.sendPort)
..addOnExitListener(port.sendPort, response: 100);
}
Here, main isolate spawns another isolate that just sends 42 after a second. After the answer is sent, the isolate terminates, causing the exit listener to fire. Indeed, the program outputs 42 and then outputs 100, so the isolate terminates.
But the Dart program still keeps running. Why's that?