-
Notifications
You must be signed in to change notification settings - Fork 205
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
Infer return types of Future.wait
independently?
#2838
Comments
Thank you, that seems what I was looking for! If I understood correctly that extension would be available on SDK/core level right? If yes does anybody knows if is it already available on dart 3 (master) so I can experiment it? |
I'm not sure it's entirely decided whether the extension goes into the SDK or into Here's a quick version to play with. import "dart:async";
extension FutureRecordWait3<R, S, T> on (Future<R>, Future<S>, Future<T>) {
/// Process futures in parallel.
///
/// If any of the futures throw,
/// the returned future throws a [ParallelAwaitError],
/// with a `(R?, S?, T?)` containing the successful values or null,
/// and an `(AsyncError?, AsyncError?, AsyncError?)` containing
/// the error results or `null`.
Future<(R, S, T)> operator ~() {
var result = Completer<(R, S, T)>.sync();
R? v1;
AsyncError? e1;
S? v2;
AsyncError? e2;
T? v3;
AsyncError? e3;
var waiting = 3;
void onDone() {
if (--waiting == 0) {
if (e1 == null && e2 == null && e3 == null) {
result.complete((v1 as R, v2 as S, v3 as T));
} else {
result.completeError(ParallelWaitError<
(R?, S?, T?), (AsyncError?, AsyncError?, AsyncError?)>(
(v1, v2, v3), (e1, e2, e3)));
}
}
}
$1.then((v) {
v1 = v;
onDone();
}, onError: (e, s) {
e1 = AsyncError(e, s);
onDone();
});
$2.then((v) {
v2 = v;
onDone();
}, onError :(e, s) {
e2 = AsyncError(e, s);
onDone();
});
$3.then((v) {
v3 = v;
onDone();
}, onError: (e, s) {
e3 = AsyncError(e, s);
onDone();
});
return result.future;
}
}
class ParallelWaitError<V extends Record, E extends Record>
extends Error {
final V values;
final E errors;
ParallelWaitError(this.values, this.errors);
} which you can use like:
(The real code will have some abstractions to help with avoiding the code repetition.) |
With records coming to dart, would be possible to define a way to infer each type separately?
RecordWithOnlyFutures
andRecordWithResolvedFutures<T extends RecordWithOnlyFutures>
are just a placeholders for a internal solution.To be honest, I really don't know if this is possible to implement, since it seems a little hard to represent this type, but is a step forward to a safer typing. I really hate to cast different future types like this:
The text was updated successfully, but these errors were encountered: