Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const std = @import("std");
- const warn = std.debug.warn;
- const allocator = std.debug.global_allocator;
- fn foo(n: u32) void {
- warn("{} * 2 = {}\n", n, n * 2);
- }
- pub fn main() !void {
- var is_finished: bool = false;
- var handle = try async<allocator> slow_action(&is_finished);
- var mapped = try async<allocator> map(u32, void, handle, foo, &is_finished);
- while(!is_finished) {
- resume mapped;
- }
- cancel mapped;
- }
- async fn map(comptime T: type, comptime U: type, p: promise-> T, f: fn(T) U, is_finished: *bool) void {
- while(!is_finished.*) {
- resume p;
- }
- var result = await p;
- f(result);
- }
- async fn slow_action(is_finished: *bool) u32 {
- suspend;
- var n: usize = 0;
- while(n < 10): (n += 1) {
- suspend;
- }
- is_finished.* = true;
- return 10;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement