develooper Front page | perl.perl5.porters | Postings from August 2021

Re: Review of `defer` (was: FINALLY - ready for comments/review)

Thread Previous | Thread Next
From:
Paul "LeoNerd" Evans
Date:
August 6, 2021 18:56
Subject:
Re: Review of `defer` (was: FINALLY - ready for comments/review)
Message ID:
20210806195639.77c8a6b9@shy.leonerd.org.uk
On Fri, 6 Aug 2021 13:42:22 -0500
David Nicol <davidnicol@gmail.com> wrote:

> wondering how defer interacts with whatever, if anything, we've got
> in Perl implementing the "promise" pattern.
> 
> TLDR: it doesn't.

Since defer blocks live on the savestack, they'll interact just fine
with the async/await syntax provided by Future::AsyncAwait:

  use feature qw( say signatures defer );
  use Future;
  use Future::AsyncAwait;

  async sub x ($f) {
    say "1";
    defer { say "4"; }
    await $f;
    say "3";
  }

  my $f1 = Future->new;

  say "0";
  my $fret = x($f1);

  say "2";
  $f1->done;

  say "5";

Results in all 6 messages being printed in numerical order -
specifically note that 4 comes after both 2 and 3.

-- 
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk      |  https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/  |  https://www.tindie.com/stores/leonerd/

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About