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); });

In reply to Re: Making a Promise by Corion
in thread Making a Promise by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.