Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Producer -> consumer chain? #82

Open
OnkelTem opened this issue Nov 20, 2017 · 1 comment
Open

Producer -> consumer chain? #82

OnkelTem opened this issue Nov 20, 2017 · 1 comment

Comments

@OnkelTem
Copy link

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?

@alexeyshockov
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants