mirror of
https://github.com/danog/amp.git
synced 2024-11-26 20:15:00 +01:00
Add Iterator\collect
This commit is contained in:
parent
6be5d4bfc5
commit
22a8332261
@ -451,6 +451,7 @@ namespace Amp\Iterator
|
||||
use Amp\Iterator;
|
||||
use Amp\Producer;
|
||||
use Amp\Promise;
|
||||
use function Amp\call;
|
||||
use function Amp\coroutine;
|
||||
use function Amp\Internal\createTypeError;
|
||||
|
||||
@ -616,4 +617,23 @@ namespace Amp\Iterator
|
||||
|
||||
return $emitter->iterate();
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects all items from an iterator into an array.
|
||||
*
|
||||
* @param Iterator $iterator
|
||||
*
|
||||
* @return Promise<array>
|
||||
*/
|
||||
function collect(Iterator $iterator): Promise
|
||||
{
|
||||
return call(function () use ($iterator) {
|
||||
$array = [];
|
||||
while (yield $iterator->advance()) {
|
||||
$array[] = $iterator->getCurrent();
|
||||
}
|
||||
|
||||
return $array;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user