ziglings/exercises/085_async2.zig

29 lines
683 B
Zig
Raw Normal View History

2021-05-12 21:04:58 -04:00
//
// So, 'suspend' returns control to the place from which it was
2021-05-25 11:51:58 -04:00
// called (the "call site"). How do we give control back to the
2021-05-12 21:04:58 -04:00
// suspended function?
//
// For that, we have a new keyword called 'resume' which takes an
// async function invocation's frame and returns control to it.
//
// fn fooThatSuspends() void {
2021-05-24 15:55:03 -04:00
// suspend {}
2021-05-12 21:04:58 -04:00
// }
//
// var foo_frame = async fooThatSuspends();
// resume foo_frame;
//
// See if you can make this program print "Hello async!".
//
const print = @import("std").debug.print;
pub fn main() void {
var foo_frame = async foo();
}
fn foo() void {
print("Hello ", .{});
2021-05-24 15:55:03 -04:00
suspend {}
2021-05-12 21:04:58 -04:00
print("async!\n", .{});
}