| 1 | package Promise::AsyncAwait; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 1 | our $VERSION = '0.01'; | |
| 7 | |||
| 8 | =encoding utf-8 | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | Promise::AsyncAwait - Async/await with promises | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | use Promise::AsyncAwait; | ||
| 17 | |||
| 18 | async sub get_number_plus_1 { | ||
| 19 | my $number = await _get_number_p(); | ||
| 20 | |||
| 21 | return 1 + $number; | ||
| 22 | } | ||
| 23 | |||
| 24 | my $p = get_number_plus_1()->then( sub { say "number: " . shift } ); | ||
| 25 | |||
| 26 | … and then use whatever mechanism you will for “unrolling” C<$p>. | ||
| 27 | |||
| 28 | =head1 DESCRIPTION | ||
| 29 | |||
| 30 | L<Future::AsyncAwait> implements JavaScript-like L<async|https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function>/L<await|https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await> | ||
| 31 | semantics | ||
| 32 | for Perl, but it defaults to using CPAN’s L<Future> rather than promises. | ||
| 33 | The two are similar but incompatible. | ||
| 34 | |||
| 35 | Use this module for a promise-oriented async/await instead. It’s actually | ||
| 36 | just a shim around Future::AsyncAwait that feeds it configuration options | ||
| 37 | for L<Promise::XS> promises rather than Future. This yields a friendlier | ||
| 38 | (and likely faster!) experience for those more accustomed to JavaScript | ||
| 39 | promises than to CPAN Future. | ||
| 40 | |||
| 41 | This should work with most CPAN promise implementations. | ||
| 42 | |||
| 43 | =cut | ||
| 44 | |||
| 45 | 1 | ▶ | use Promise::XS 0.13; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 46 | |||
| 47 | 1 | ▶ | use constant _MIN_FAA_VERSION => 0.47; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 48 | |||
| 49 | 1 | our @ISA; | |
| 50 | |||
| 51 | 1 | ▶ | BEGIN { |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 52 | |||
| 53 | # Future::AsyncAwait loads Future, which we don’t need. | ||
| 54 | 1 | local $INC{'Future.pm'} = __FILE__; | |
| 55 | 1 | ▶ | local *Future::VERSION = sub { '999.99' }; |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 56 | |||
| 57 | 1 | @ISA = ('Future::AsyncAwait'); | |
| 58 | 1 | require Future::AsyncAwait; | |
| 59 | 1 | Future::AsyncAwait->VERSION(_MIN_FAA_VERSION); | |
| 60 | } | ||
| 61 | |||
| 62 | 1 | ▶ | sub import { |
|
Subroutine
import: called CC=1 SCAR=0.0
| |||
| 63 | 1 | return $_[0]->SUPER::import( | |
| 64 | future_class => 'Promise::XS::Promise', | ||
| 65 | @_[1 .. $#_], | ||
| 66 | ); | ||
| 67 | } | ||
| 68 | |||
| 69 | =head1 LICENSE & COPYRIGHT | ||
| 70 | |||
| 71 | Copyright 2021 Gasper Software Consulting. All rights reserved. | ||
| 72 | |||
| 73 | This library is licensed under the same license as Perl. | ||
| 74 | |||
| 75 | =cut | ||
| 76 | |||
| 77 | 1 | 1; | |