Data-Promise

 view release on metacpan or  search on metacpan

lib/Data/Promise.pm  view on Meta::CPAN

package Data::Promise;

=head1 NAME

Data::Promise - simple promise like interface

=head1 SYNOPSIS

  use Modern::Perl;
  use Data::Promose;

  my $p=new Data::Promise(cb=>sub {
    my ($resolve,$reject)=@_;

    if(...) {
      # pass context
      $resolve->('ok');
    } else {
      $reject->('something went wrong');
    }
  });

  sub pass_function { ... }
  sub fail_function { ... }
  $p->then(\&pass_function,\&fail_function);


  # delayed example
  my $p=new Data::Promise(
    delayed=>1,
    cb=>sub {

    if(...) {
      # pass context
      $resolve->('ok');
    } else {
      $reject->('something went wrong');
    }
  });

  $p->then(\&pass_function,\&fail_function);
  # pass and fail functions will not be called until now
  $p->do_resolve;

  ## create a rejected promise
  my $p=Data::Promise->reject(42);

  # you can be sure your fail funtion will be called
  $p->then(\&pass_function,\&fail_function);

  ## create a resolved promise
  my $p=Data::Promise->resolve(42);

  # you can be sure your pass funtion will be called
  $p->then(\&pass_function,\&fail_function);

=head1 DESCRIPTION

A light and simple Promise object based on the current es6 implementation.   This promise object class was written to mimic how promise(s) are implemnted in the wild.  This may or may not be the class you are looking for.

=cut

our $VERSION=0.001;

use Modern::Perl;
use Moo;
use MooX::Types::MooseLike::Base qw(:all);

use namespace::clean;

=head1 OO Constructor Arguments

=over 4

=item * cb=>sub { my ($resovle,$reject)=@_ }

The callback function used to resolve the object.  If no function is passed in then the object will never resolve!

=cut

has cb=>(
  isa=>CodeRef,
  default=>\&_build_stub,
  is=>'ro',
);

=item * delayed=>0|1

This enables or disables manual control over when your cb function will be called.  The default is false.

=cut

has delayed=>(
  isa=>Bool,
  is=>'ro',
  default=>0,
);

=back

=cut

has _jobs=>(
  isa=>ArrayRef,
  is=>'ro',
  default=>sub {[]},
);

has _finally=>(
  isa=>ArrayRef,
  is=>'ro',
  default=>sub {[]},
);

has _then_catch=>(
  isa=>ArrayRef,
  is=>'ro',
  default=>sub {[]},
);

has _pending=>(
  is=>'rw',
);

has _result=>(

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 2.076 seconds using v1.00-cache-2.02-grep-82fe00e-cpan-f5108d614456 )