From d09ae39714bbca6e12c67939509a36faa6a76198 Mon Sep 17 00:00:00 2001 From: Dave Gauer Date: Thu, 27 May 2021 19:04:11 -0400 Subject: Add ex089 Async 6 --- exercises/089_async6.zig | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 exercises/089_async6.zig (limited to 'exercises') 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"; +} -- cgit v1.2.3-ZIG