Data::Promise - simple promise like interface
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);
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.
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!
delayed=>0|1
This enables or disables manual control over when your cb function will be called. The default is false.
Used as a state checking interface, this method returns true if the promise is still being resolved, false if it is not.
This method provides a way to attach functions that will be called when the object is either rejected or resovled.
This is really a wrapper function for: $p->then(undef,\&reject);
Creates a rejected promise with @args as the rejected data.
Creates a resolved promise with @args as the resolved data.
Allows the addition of functions that will be called once the object is resolved. The functions will recive no arguments, and are called reguardless of the resolved or rejected state.
When the promise is constructed in a delayed state, this method must be called to activate the cb method.
Michael Shipper <AKALINUX@CPAN.ORG>
To install Data::Promise, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Data::Promise
CPAN shell
perl -MCPAN -e shell install Data::Promise
For more information on module installation, please visit the detailed CPAN module installation guide.