Skip to content

Commit 6165d43

Browse files
committed
Add start { } to control flow
1 parent a7845c0 commit 6165d43

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

doc/Language/control.pod6

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,33 @@ parenthesize a statement if it is the last thing in an expression:
128128
=for code :skip-test<syntax error>
129129
3, if 1 { 2 } ; # Syntax error
130130
131-
...which brings us to C<if>.
131+
=head1 X<start|control flow,start>
132+
133+
The simplest way to run a block B<asynchronously> is by writing C<start>
134+
before it:
135+
136+
=for code
137+
start { sleep 1; say "done" }
138+
say "working";
139+
# working, done
140+
141+
Note that you need a space between the C<start> and the block.
142+
143+
The C<start {...}> immediately returns a C<Promise> that can be safely ignored
144+
if you are not interested in the result of the block. If you B<are> interested
145+
in the final value of the block, you can call the C<.result> method on the
146+
returned promise. So:
147+
148+
my $promise = start { sleep 10; 42 }
149+
# ... do other stuff
150+
say "The result is $promise.result()";
151+
152+
If the code inside the block has not finished, the call to C<.result> will
153+
wait until it is done.
154+
155+
A C<start> may also be used on a bare statement (without curly braces).
156+
This is mainly just useful when calling a subroutine / method on an object
157+
is the only thing to do asynchronously.
132158
133159
=head1 X<if|control flow,if>
134160

0 commit comments

Comments
 (0)