Skip to content

Instantly share code, notes, and snippets.

@yongjhih
Last active May 14, 2024 21:14
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);
}
}
class UserProfile {
final String? a;
final String? b;
final String? c;
final int? d;
const UserProfile([this.a, this.b, this.c, this.d]);
@override String toString() => "$a, $b, $c, $d";
}
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));
}();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment