Skip to content

Instantly share code, notes, and snippets.

@yongjhih
Last active November 6, 2024 05:09
Show Gist options
  • Save yongjhih/fd5929f9220ad538b244943722e1c3aa to your computer and use it in GitHub Desktop.
Save yongjhih/fd5929f9220ad538b244943722e1c3aa to your computer and use it in GitHub Desktop.
extension FutureZipX<T> on Future<T> {
Future<(T, T2)> zipWith<T2>(Future<T2> future2) async {
late T result1;
late T2 result2;
await Future.wait(
[then((it) => result1 = it), future2.then((it) => result2 = it)]);
return (result1, result2);
}
}
extension TupleFuture2X<T1, T2> on (Future<T1>, Future<T2>) {
Future<(T1, T2)> wait() async {
final (a, b) = this;
return (await a, await b);
}
}
class UserProfile {
final String? a;
final String? b;
final String? c;
final int? d;
final bool? e;
const UserProfile([this.a, this.b, this.c, this.d, this.e]);
@override String toString() => "$a, $b, $c, $d, $e";
}
void main() async {
await () async {
final (a, b) =
await Future.value("a")
.zipWith(Future.value("b"));
print(UserProfile(a, b));
}();
await () async {
final ((a, b), c) =
await Future.value("a")
.zipWith(Future.value("b"))
.zipWith(Future.value("c"));
print(UserProfile(a, b, c));
}();
await () async {
final (((a, b), c), d) =
await Future.value("a")
.zipWith(Future.value("b"))
.zipWith(Future.value("c"))
.zipWith(Future.value(4));
print(UserProfile(a, b, c, d));
}();
await () async {
final (a, d) = await (
Future.value("a1"),
Future.value(4),
).wait();
print(UserProfile(a, "b", "c", d));
}();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment