The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Promises6::Deferred

VERSION

version 0.003

SYNOPSIS

  use Evo::Base -strict;
  use Promises6::Builder;
  use Promises6::Deferred;

  my $bldr = Promises6::Builder->new(deferred_class => 'Promises6::Deferred');
  my $deferred = $bldr->deferred;
  my $promise  = $deferred->promise;

  $promise->then(
    sub($v) { say "resolved $v" },
    sub($r) { say "rejected $r" },
    sub($p) { say "Progress $p" }
  );

  $deferred->notify(10);
  $deferred->notify(20);
  $deferred->resolve('val');

DESCRIPTION

  This object represents a state for the promise an a links to other promises,
  created by L<Promises6/"then"> method by sending messages to L</"subscribers">
  when the state changes

  Promises/A+ says that than could be called several times on the same promise.
  You can consider that it's a sheperd of the flock of promises that have
  been called with then on the same parent promise.

  So each C<promise> has only one C<deferred>, while each C<deferred> can have
  one or many C<promises>

  Not all method should be used directly.

ATTRIBUTES

result, is_adaptor, subscribers

  Internal attributes

builder

An instance of Promises6::Builder

METHODS

promise

  Builds a promise associated with this object

resolve

  $d->resolve('value')

Starts the resolution procedure https://github.com/promises-aplus/promises-spec#the-promise-resolution-procedure If value were provided, fulfills all connected promises with that value invoking "change_state" (which sends messages about that to all subscribers)

If value is a thenable object or promise, it will follow it.

reject

  $deferred->reject('reason');

Rejects all connected promises with the reason and send messages to subsribers. If value is a thenable object or a promise, it WON'T follow it, because this method changes a state immidiately. So rejecting promises with other promises is a mistake.

notify

Notify subscribers about the progress or something else. Can be called many times before deferred object is resolved

  $deferred->notify(10);
  $deferred->notify(20);

It's not a part of the promises/A+. The behaviour is almast the same as JS Q. If promise is PENDING, all subscribers will be notified with given value by calling 3rd argument of then. This invocation should return a value, which will be passed to the next member of chain.

The difference with JS Q is if on_notify causes an error, the promises will be rejected but this could be changed in the future. (Q does nothing with that but I think it's a mistake in implementation)

state

  Returns current state of deferred object.

change_state

  Immidiately change a state.
  This is an internal method and shouldn't be used directly

broadcast

  Send message to all subscribers
  This is an internal method and shouldn't be used directly

send_msg

  Sends a message to the listener. This method should be subclassed to run
  out from recursions and avoid "deep recursion" problems by sending a message
  in the "next tick".
  This is an internal method and shouldn't be used directly

subscribe

  Subscribe a listener, by adding it to the subscribers list
  This is an internal method and shouldn't be used directly

AUTHOR

alexbyk <alexbyk.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2015 by alexbyk.

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