Skip to content

How LazyPromise is supposed to be used with Deferred?  #95

@makasim

Description

@makasim

I am performing an RPC call over MQ. and I'd like to return the control to the code that invoked my method. The $reply->receive() call must be postponed as long as possible. It is blokcing operation and it returns the result. I cannot call $deferred->resolve immidiatly so I am thinking of doing something with LazyPromise. Something like this:

<?php

public function __invoke(Message $message, Deferred $deferred = null):Promise
{
    // do some stuff which results in $reply var. 

        return new LazyPromise(function() use ($deferred, $reply) {
            try {
                $value = JSON::decode($reply->receive($this->replyTimeout)->getBody());

                $deferred->resolve($value);
            } catch (TimeoutException $e) {
                $deferred->reject($e->getMessage());
            }

            return $deferred->promise();
        });
    }

Is this the right way to go? Is there a better solution?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions