Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: timeoutToNull extension method #145

Open
jathak opened this issue Dec 9, 2020 · 2 comments
Open

Feature request: timeoutToNull extension method #145

jathak opened this issue Dec 9, 2020 · 2 comments
Labels
type-enhancement A request for a change that isn't a bug

Comments

@jathak
Copy link

jathak commented Dec 9, 2020

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

@nex3
Copy link
Member

nex3 commented Dec 9, 2020

Note that this is in the same tradition as the collection package's firstWhereOrNull() et al extension methods.

@lrhn
Copy link
Member

lrhn commented May 12, 2021

It does make sense, yes.

@lrhn lrhn added the type-enhancement A request for a change that isn't a bug label May 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

3 participants