September 2017
Beginner
402 pages
9h 52m
English
Another interesting feature of promises is that they can return a result. This result is calculated by the code block, and the last calculated value is what is returned. Consider the following example:
my $promise = Promise.start({ sleep 1; 'Result'; # no return keyword!});await $promise;say $promise.result;
Here, the code block returns a string, Result, which is then printed using the result method called on the $promise variable.
It logically follows that the result is only available after the promise is kept. In the previous example, an explicit await was used. Actually, it is redundant, because the call of the result method will wait until the code is completed. Thus, the code may be simplified as follows:
Read now
Unlock full access