promises icon indicating copy to clipboard operation
promises copied to clipboard

Producer -> consumer chain?

Open OnkelTem opened this issue 8 years ago • 1 comments

I don't really understand can I implement producer-consumer scheme using this library?

For example, imagine a producing function which makes some values over time, and another consuming function which takes the values. Is there a way to implement this using Promises?

OnkelTem avatar Nov 20 '17 09:11 OnkelTem

The idea of promises is to represent one values per a promise. For generating values over time PHP's generators are more suitable.

$consumerFn = function () {
    while ($value = yield) {
        // Process the value somehow.
        var_dump($value);
    }
};

$consumer = $consumerFn();

// Produce values anywhere in your code.
$consumer->send(1);
$consumer->send(new stdClass());

It's a basic example 1-to-1 producer/subscriber. If you want to implement more complicated schemes, it's better to use events and subscribers, like Symfony's EventDispatcher.

alexeyshockov avatar Nov 20 '17 16:11 alexeyshockov