Skip Menu |

This queue is for tickets about the future CPAN distribution.

Report information
The Basics
Id: 131094
Status: resolved
Priority: 0/
Queue: future

People
Owner: Nobody in particular
Requestors: felipe [...] felipegasper.com
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: 0.45



Subject: then() seems incorrectly documented
Date: Wed, 27 Nov 2019 05:43:50 -0500
To: bug-future [...] rt.cpan.org
From: Felipe Gasper <felipe [...] felipegasper.com>
Currently the POD says: ----- then (2 arguments) $future = $f1->then( \&done_code, \&fail_code ) The then method can also be passed the $fail_code block as well, giving a combination of then and else behaviour. This operation is designed to be compatible with the semantics of other future systems, such as Javascript's Q or Promises/A libraries. ----- … yet this isn’t how then() works, as a quick example shows: ----- perl -MFuture -e'my $f = Future->done(123); my $f2 = $f->then( sub { print 123 } )->then( sub { print 456 } )' 123 ----- It should print “123456”, but instead it only prints “123”. A quick comparison with node.js’s implementation: ----- Show quoted text
> Promise.resolve(123).then( () => console.log(123) ).then( () => console.log(234) );
Promise { <pending> } Show quoted text
> 123
234 ----- If Future::then() is described as “compatible” with Promise/A, I would think it would obey the same semantics and immediately resolve the returned Future. Given, though, that Future is already widely used, I assume that its current functionality is as intended; hence, the documentation should indicate what Future::then() does differently from then() on promises. Thank you! -FG
On Wed Nov 27 06:28:37 2019, felipe@felipegasper.com wrote: Show quoted text
> perl -MFuture -e'my $f = Future->done(123); my $f2 = $f->then( sub { > print 123 } )->then( sub { print 456 } )' > 123
Oh this is awkward. That's supposed to print an error that the first `then` block didn't return a Future. Instead nothing happened - the error has gone missing :( In any case, as every chaining code block is supposed to yield a Future, your example didn't do that so it doesn't run properly. Instead: $ perl -Mwarnings -MFuture -e'my $f = Future->done(123); my $f2 = $f->then( sub { print 123; Future->done } )->then( sub { print 456 } )' 123456 I'll investigate why the error went missing -- Paul Evans
On Wed Nov 27 08:11:12 2019, PEVANS wrote: Show quoted text
> On Wed Nov 27 06:28:37 2019, felipe@felipegasper.com wrote:
> > perl -MFuture -e'my $f = Future->done(123); my $f2 = $f->then( sub { > > print 123 } )->then( sub { print 456 } )' > > 123
> > Oh this is awkward. That's supposed to print an error that the first > `then` block didn't return a Future. Instead nothing happened - the > error has gone missing :(
Ohright; it's not thrown as an inline error, but instead becomes the failure state of the returned Future. Since you just discarded that, it doesn't appear. Try ->get'ing it: $ perl -Mwarnings -MFuture -e'my $f = Future->done(123); my $f2 = $f->then( sub { print 123 } )->then( sub { print 456 } )->get' Expected __ANON__(-e line 1) to return a Future at -e line 1. 123 $ perl -Mwarnings -MFuture -e'my $f = Future->done(123); my $f2 = $f->then( sub { print 123; Future->done } )->then( sub { print 456 } )->get' Expected __ANON__(-e line 1) to return a Future at -e line 1. 123456 $ perl -Mwarnings -MFuture -e'my $f = Future->done(123); my $f2 = $f->then( sub { print 123; Future->done } )->then( sub { print 456; Future->done } )->get' 123456 -- Paul Evans
Subject: Re: [rt.cpan.org #131094] then() seems incorrectly documented
Date: Wed, 27 Nov 2019 08:49:04 -0500
To: bug-future [...] rt.cpan.org
From: Felipe Gasper <felipe [...] felipegasper.com>
Show quoted text
> Le 27 nov. 2019 à 08:14, Paul Evans via RT <bug-future@rt.cpan.org> a écrit : > > Ohright; it's not thrown as an inline error, but instead becomes the failure state of the returned Future. Since you just discarded that, it doesn't appear. Try ->get'ing it: > > $ perl -Mwarnings -MFuture -e'my $f = Future->done(123); my $f2 = $f->then( sub { print 123 } )->then( sub { print 456 } )->get' > Expected __ANON__(-e line 1) to return a Future at -e line 1. > 123 > > $ perl -Mwarnings -MFuture -e'my $f = Future->done(123); my $f2 = $f->then( sub { print 123; Future->done } )->then( sub { print 456 } )->get' > Expected __ANON__(-e line 1) to return a Future at -e line 1. > 123456 > > $ perl -Mwarnings -MFuture -e'my $f = Future->done(123); my $f2 = $f->then( sub { print 123; Future->done } )->then( sub { print 456; Future->done } )->get' > 123456
Ah, thank you for clarifying! Mostly what I’m getting at, though, is that this interface appears to work quite differently from then() as promise objects implement it. With promises, the return from the then() callback is given to the next promise down the chain, and only if the then() callback throws/dies is the returned promise a failure. That being the case, should the documentation be updated to describe a bit more fully what Future::then() does? Thank you!
On Wed Nov 27 08:49:16 2019, felipe@felipegasper.com wrote: Show quoted text
> Mostly what I’m getting at, though, is that this interface appears to > work quite differently from then() as promise objects implement it. > With promises, the return from the then() callback is given to the > next promise down the chain, and only if the then() callback > throws/dies is the returned promise a failure.
But that is what is happening here. $ perl -MFuture -E 'say Future->done("First") ->then(sub { say "Previous result was $_[0]"; Future->done("second") }) ->then(sub { say "The next result was $_[0]"; Future->done }) ->get' Previous result was First The next result was second -- Paul Evans
Subject: Re: [rt.cpan.org #131094] then() seems incorrectly documented
Date: Tue, 28 Jan 2020 19:40:24 -0500
To: bug-future [...] rt.cpan.org
From: Felipe Gasper <felipe [...] felipegasper.com>
Show quoted text
> On Jan 27, 2020, at 9:32 PM, Paul Evans via RT <bug-future@rt.cpan.org> wrote: > > <URL: https://rt.cpan.org/Ticket/Display.html?id=131094 > > > On Wed Nov 27 08:49:16 2019, felipe@felipegasper.com wrote:
>> Mostly what I’m getting at, though, is that this interface appears to >> work quite differently from then() as promise objects implement it. >> With promises, the return from the then() callback is given to the >> next promise down the chain, and only if the then() callback >> throws/dies is the returned promise a failure.
> > But that is what is happening here. > > $ perl -MFuture -E 'say Future->done("First") > ->then(sub { say "Previous result was $_[0]"; Future->done("second") }) > ->then(sub { say "The next result was $_[0]"; Future->done }) > ->get' > Previous result was First > The next result was second
The *return* from the callback is what promises propagate, though. Future appears to require another Future in order to propagate and discard the callback’s return, which diverges from the usual promise interface: ===== Show quoted text
> node
Welcome to Node.js v12.13.1. Type ".help" for more information. Show quoted text
> Promise.resolve("first").then( v => { console.log(v); return "second" } ).then( v => { console.log(v); } )
Promise { <pending> } Show quoted text
> first
second Show quoted text
> perl -MPromises::Deferred -e'my $d = Promises::Deferred->new(); $d->resolve("first"); $d->promise()->then( sub { print shift; return "second" } )->then( sub { print shift } )'
firstsecond Show quoted text
> perl -MPromise::ES6 -e'Promise::ES6->resolve("first")->then( sub { print shift; return "second" } )->then( sub { print shift } )'
firstsecond Show quoted text
> perl -MFuture -e'Future->done("first")->then( sub { print shift; return "second" } )->then( sub { print shift } )'
Calling ->then in void context at -e line 1. first ===== I’m not proposing that Future be changed, but merely that the documentation clarify this divergence. -FG
Behaviour was changed in 0.45 to accept non-Future return values which are silently upgraded. -- Paul Evans