Skip to content

Commit ac679f5

Browse files
committed
- Added Future.
1 parent ddae090 commit ac679f5

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

scripts/future/future.pl

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env perl
2+
3+
use v5.38;
4+
use Future;
5+
use IO::Async::Loop;
6+
7+
my $loop = IO::Async::Loop->new;
8+
9+
sub fetch {
10+
my $future = $loop->new_future;
11+
$loop->delay_future(after => 1)->on_done(sub {
12+
$future->done({ data => "Fetched data" });
13+
});
14+
return $future;
15+
}
16+
17+
sub process($data) {
18+
my $future = $loop->new_future;
19+
$loop->delay_future(after => 1)->on_done(sub {
20+
$data->{processed} = "Processed: " . $data->{data};
21+
$future->done($data);
22+
});
23+
return $future;
24+
}
25+
26+
sub save($data) {
27+
my $future = $loop->new_future;
28+
$loop->delay_future(after => 1)->on_done(sub {
29+
$data->{saved} = "Saved: " . $data->{processed};
30+
$future->done($data);
31+
});
32+
return $future;
33+
}
34+
35+
sub display($data) {
36+
say "Displaying data: " . $data->{saved};
37+
return Future->done;
38+
}
39+
40+
sub error($message) {
41+
say "Error: $message";
42+
return Future->done;
43+
}
44+
45+
$loop->await(
46+
fetch()
47+
->then( sub { return process(@_); })
48+
->then( sub { return save(@_); })
49+
->then( sub { return display(@_); })
50+
->catch(sub { return error(@_); })
51+
);

0 commit comments

Comments
 (0)