async
async copied to clipboard
Feature request: timeoutToNull extension method
It would be useful to have a method on a Future<T> that returns a Future<T?> that resolves to null if it doesn't complete within a timeout.
This would be useful for cases like _exitCodeOrNull in test_process, which looked like this prior to null safety:
Future<int> get _exitCodeOrNull async =>
await exitCode.timeout(Duration.zero, onTimeout: () => null);
but requires this when migrating to null safety, as exitCode is a Future<int> but we need a Future<int?>:
Future<int?> get _exitCodeOrNull => exitCode
.then<int?>((value) => value)
.timeout(Duration.zero, onTimeout: () => null);
With an extension method, this could be simplified to:
Future<int?> get _exitCodeOrNull => exitCode.timeoutToNull(Duration.zero);
cc: @nex3
Note that this is in the same tradition as the collection package's firstWhereOrNull() et al extension methods.
It does make sense, yes.