Promises::Sub(3)

πŸ“¦
version 1.05 Β· 2 versions
, in fedora 43

πŸ’– Promises::Sub πŸ’–

okay so bestie, welcome to Promises::Sub(3) β€” it's a User Contributed Perl Documentation page, and it's literally the glow-up your functions have been waiting for. πŸ¦‹


✨ NAME ✨

Promises::Sub β€” Turns functions into promises πŸ‘‘

no cap, that's the whole vibe.


🌸 VERSION 🌸

version 1.05

(it's giving established. it's giving stable. we love her. πŸ’…)


πŸŽ€ SYNOPSIS πŸŽ€

okay so here's the moment you've been waiting for, bestie. look at this:

use Promises 'deferred';
use parent 'Promises::Sub';

sub shall_concat :Defer {
    join ' ', @_;
}

my @promises = map { deferred } 1..2;

my @results = (
    shall_concat( @promises ),
    shall_concat( 'that is', $promises[1] ),
    shall_concat( 'this is', 'straight up' ),
);

say "all results are promises";

$_->then(sub { say @_ } ) for @results;
# prints 'this is straight up'

say "two results are still waiting...";

$promises[1]->resolve( 'delayed' );
# prints 'this is delayed'

say "only one left...";

$promises[0]->resolve( 'finally the last one, that was' );
# prints 'finally the last one, that was delayed'

see how the one with no promises in it just went? yeah. 'this is straight up' printed immediately because nothing was holding it back. the other two were just sitting there like πŸ‘€ waiting on their promises to resolve. iconic behavior honestly.


🫢 DESCRIPTION 🫢

so the tea is: any function you tag with :Defer gets turned into a promise. automatic. no extra steps. so you can literally do this:

sub add :Defer { $_[0] + $_[1] }

add( 1,2 )->then(sub { say "the result is ", @_ } );

and beyond that β€” plot twist β€” if any of the arguments you pass in are promises themselves, the function call will wait until those promises are fulfilled before it even runs. it's patient. it's polite. it's not going anywhere until the vibes are right.

my $number = deferred;

add( 1, $number )->then(sub { say "result: ", @_ } );

# $number is not fulfilled yet, nothing is printed

$number->resolve(47);
# prints 'result: 48'
⚠️ okay important, do not skip this part bestie ⚠️ to use the :Defer attribute, you have to do use parent 'Promises::Sub'; and NOT use Promises::Sub; in the target namespace. like, use parent, specifically. don't do the other one. i'm serious. πŸ’€

🧚 Anonymous functions 🧚

so here's the thing β€” :Defer will not work for anonymous functions, and it will throw an exception if you try. πŸ™ˆ it's just not that kind of girl.

but don't panic! for those, you can export the function defer, which will wrap any coderef the exact same way that :Defer does. problem solved. slay. ✨

use Promises::Sub qw/ defer /;

my $promised_sub = defer sub {
    join ' ', @_;
};

my $p1 = deferred;

$promised_sub->( 'hello', $p1 )->then( sub {
    say shift;
} );

# prints nothing

$p1->resolve('world');
# => prints 'hello world'

see? defer said "i got you" and wrapped that anonymous sub right up. πŸŽ€


πŸ‘‘ AUTHOR πŸ‘‘

Stevan Little <stevan.little@iinteractive.com>

(thank you stevan, we are obsessed with your work fr fr πŸ’œ)


This software is copyright (c) 2025, 2017, 2014, 2012 by Infinity Interactive, Inc.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.


okay that's the whole page bestie, you ate that πŸ’…βœ¨

this has been Promises::Sub, perl v5.42.0, dated 2025-07-25 β€” and it's literally a User Contributed Perl Documentation classic. mwah. πŸ“πŸ’–πŸ¦‹

πŸ“¦ where this page comes from