summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--TEMP_TODO4
-rw-r--r--build.zig4
-rw-r--r--exercises/089_async6.zig53
-rw-r--r--patches/patches/089_async6.patch6
4 files changed, 63 insertions, 4 deletions
diff --git a/TEMP_TODO b/TEMP_TODO
deleted file mode 100644
index 9b74397..0000000
--- a/TEMP_TODO
+++ /dev/null
@@ -1,4 +0,0 @@
-071 - inline for - loop through struct fields
-072 - inline while (see lib/std/fmt.zig)
-073 - comptime block
-074 - quiz 8 - revisit 058_quiz7 and make those connections programatically?
diff --git a/build.zig b/build.zig
index 043bb2c..fe65a96 100644
--- a/build.zig
+++ b/build.zig
@@ -434,6 +434,10 @@ const exercises = [_]Exercise{
.main_file = "088_async5.zig",
.output = "Example Title.",
},
+ .{
+ .main_file = "089_async6.zig",
+ .output = ".com: Example Title, .org: Example Title.",
+ },
};
/// Check the zig version to make sure it can compile the examples properly.
diff --git a/exercises/089_async6.zig b/exercises/089_async6.zig
new file mode 100644
index 0000000..d1681f7
--- /dev/null
+++ b/exercises/089_async6.zig
@@ -0,0 +1,53 @@
+//
+// The power and purpose of async/await becomes more apparent
+// when we do multiple things concurrently. Foo and Bar do not
+// depend on each other and can happen at the same time, but End
+// requires that they both be finished.
+//
+// +---------+
+// | Start |
+// +---------+
+// / \
+// / \
+// +---------+ +---------+
+// | Foo | | Bar |
+// +---------+ +---------+
+// \ /
+// \ /
+// +---------+
+// | End |
+// +---------+
+//
+// We can express this in Zig like so:
+//
+// fn foo() u32 { ... }
+// fn bar() u32 { ... }
+//
+// // Start
+//
+// var foo_frame = async foo();
+// var bar_frame = async bar();
+//
+// var foo_value = await foo_frame;
+// var bar_value = await bar_frame;
+//
+// // End
+//
+// Please await TWO page titles!
+//
+const print = @import("std").debug.print;
+
+pub fn main() void {
+ var com_frame = async getPageTitle("http://example.com");
+ var org_frame = async getPageTitle("http://example.org");
+
+ var com_title = com_frame;
+ var org_title = org_frame;
+
+ print(".com: {s}, .org: {s}.\n", .{com_title, org_title});
+}
+
+fn getPageTitle(url: []const u8) []const u8 {
+ // Please PRETEND this is actually making a network request.
+ return "Example Title";
+}
diff --git a/patches/patches/089_async6.patch b/patches/patches/089_async6.patch
new file mode 100644
index 0000000..ecbef19
--- /dev/null
+++ b/patches/patches/089_async6.patch
@@ -0,0 +1,6 @@
+44,45c44,45
+< var com_title = com_frame;
+< var org_title = org_frame;
+---
+> var com_title = await com_frame;
+> var org_title = await org_frame;