1
0
mirror of https://github.com/danog/amp.git synced 2024-11-30 04:29:08 +01:00

Add Iterator\collect

This commit is contained in:
Niklas Keller 2018-10-05 21:01:57 +02:00
parent 6be5d4bfc5
commit 22a8332261

View File

@ -451,6 +451,7 @@ namespace Amp\Iterator
use Amp\Iterator; use Amp\Iterator;
use Amp\Producer; use Amp\Producer;
use Amp\Promise; use Amp\Promise;
use function Amp\call;
use function Amp\coroutine; use function Amp\coroutine;
use function Amp\Internal\createTypeError; use function Amp\Internal\createTypeError;
@ -616,4 +617,23 @@ namespace Amp\Iterator
return $emitter->iterate(); 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;
});
}
} }