in reply to Making a Promise
In what sense does your snippet as shown not work?
Can you wrap the snippet in a small (20 lines) self-contained program that shows the problem?
Note that the call to External::Parser->run will always have happened before your function returns. This is different from the example in Promises because the http_get function is an asynchronous function which invokes the callback upon completion. If you want to move some arbitrary synchonous code to Promises, I would try to wrap a callback to that code with an object that implements a function ->result that actually calls the synchronous code and returns the result:
package Promise::Lazy; use parent 'Promises::Promise'; sub new { my( $class, $usercallback ) = @_; my $self = $class::SUPER->new(); $self->{ usercallback } = $usercallback; $self }; sub result { my( $self ) = @_; my $result = $self->{ usercallback }->(); if( $result ) { $self->resolve( $result ) } else { $self->reject(); }; }; ... somewhere in your code ... return Promise::Lazy->new(sub { External::Parser->run('--in', $file); });
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Making a Promise
by Anonymous Monk on Oct 05, 2015 at 21:44 UTC |