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
Why the first() method does not resolve the promise upon "end" event? #14
Comments
I haven't looked into the actual problem, but Amp supports ReactPHP's promises out of the box, you can just |
You're right @kelunik, thank you. Here the updated example: <?php
require 'vendor/autoload.php';
\Amp\Loop::defer(function () {
$csv = fopen('https://raw.githubusercontent.com/Rakdar/reactphp-csv/master/examples/country-codes.csv', 'rb');
$stream = new \React\Stream\ReadableResourceStream($csv, \Amp\ReactAdapter\ReactAdapter::get());
$reader = new \Rakdar\React\Csv\Reader($stream);
while ($row = yield \React\Promise\Stream\first($reader, 'data')) {
echo $row[10] .PHP_EOL;
}
});
\Amp\Loop::run(); |
Hi @mmenozzi! While this is indeed expected behavior as per https://github.com/reactphp/promise-stream#first, I think this is a very good question. The idea is that this function resolves with the first value once. If the stream ends (closes) without ever emitting a first value, this means that there can not be a first value, hence the exception. The rest of you question seems to be directed at another independent package, so I would suggest reporting this upstream and maybe add a link back to this issue. I hope this helps 👍 |
Hi @clue, Given the current implementation of promise-stream, to implement the example above I have to catch a generic RuntimeException and then ignore the exception only if its message contains “stream closed”. It would make sense if the promise-stream throws a specific exception in such case? |
I think it could make sense to special-case the @mmenozzi |
Ok @kelunik thank you anyway. |
@mmenozzi You can directly turn the reader into an iterator, that's even easier then. |
Yes this is what I would like to achieve. How can I do that without converting stream events to promises? |
@mmenozzi @kelunik Afaict this discussion does not appear to be related to this project at all, so I would like to ask you to move this discussion to another place as suggested above, thank you. @mmenozzi Let's get back to your original question: Can you elaborate on your use case where it makes sense to use |
Maybe you mean that I should replace the rakdar/reactphp-csv with a CSV stream parser which implements the Amp\Iterator interface? |
@mmenozzi Basically similar to the code I linked above. This gist might help, but I haven't tested it. If you have further questions, just ping me in |
Hi,
I'm trying to use rakdar/reactphp-csv inside an amphp/amp application and I would like to yield one CSV row at a time. To do so I use this package to transform
data
events ofrakdar/reactphp-csv
to ReactPHP promises which in turns are then converted to AMP promises. So I can yield CSV rows, one at a time. Here follows a simple test script:This code works but when the stream reaches the end it throws a "Stream closed" exception.
If I add the following inside the
first
function it works without exceptions:So, Why the first() method does not resolve the promise upon "end" event?
What do you think?
The text was updated successfully, but these errors were encountered: