PHPackages                             muqsit/asynciterator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. muqsit/asynciterator

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

muqsit/asynciterator
====================

A virion that simplifies writing tasks that traverse iterators

v1.1.4(2y ago)182.9k↓18.2%5GPL-3.0PHP

Since Jan 17Pushed 2y ago1 watchersCompare

[ Source](https://github.com/Muqsit/AsyncIterator)[ Packagist](https://packagist.org/packages/muqsit/asynciterator)[ RSS](/packages/muqsit-asynciterator/feed)WikiDiscussions pm5 Synced 1mo ago

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

AsyncIterator
=============

[](#asynciterator)

`AsyncIterator` simplifies writing asynchronous iteration tasks, such as for-eaching an iterator.

```
/** @var Plugin $plugin */
$handler = new AsyncIterator($plugin->getScheduler());
```

---

`AsyncIterator::forEach` traverses forward over an `Iterator` type and notifies handlers in the order of insertion. Handlers can be added to a `forEach` task by feeding a `Closure` to `AsyncIterator::forEach()::as()`, having the signature `function(TKey $key, TValue $value) : AsyncForeachResult`.

```
$handler->forEach(new ArrayIterator([1, 2]))
->as(function(int $key, int $value) : AsyncForeachResult{
	echo "First ", $value;
	return AsyncForeachResult::CONTINUE();
})
->as(function(int $key, int $value) : AsyncForeachResult{
	echo "Second ", $value;
	return AsyncForeachResult::CONTINUE();
});
```

```
First 1
Second 1
First 2
Second 2

```

---

By default, `AsyncIterator::forEach` traverses over 10 entries each tick. This can be changed by overriding the default parameter values of the method.

```
$entries_per_tick = 4;
$sleep_time = 1; // in ticks
AsyncIterator::forEach(new InfiniteIterator(new ArrayIterator([1, 2, 3])), $entries_per_tick, $sleep_time)
->as(function(int $key, int $value) : AsyncForeachResult{
	echo $value, ", ";
	return AsyncForeachResult::CONTINUE();
});
```

```
# First Tick:
1, 2, 3, 1

# Second Tick:
2, 3, 1, 2

...

```

---

Completion listeners are triggered when a foreach task successfully completes. This is determined by the return value of `Iterator::valid()` (i.e., `$completed = !Iterator::valid()`) of the iterator that is passed to `AsyncIterator::forEach`.

```
$handler->forEach(new ArrayIterator([1, 2, 3]))
->as(function(int $key, int $value) : AsyncForeachResult{
	echo $value;
	return AsyncForeachResult::CONTINUE();
})
->onCompletion(function() : void{ echo "Completed"; })
```

---

Handlers have the ability to either continue, interrupt or cancel the traversal by returning either `AsyncForeachResult::CONTINUE()`, `AsyncForeachResult::INTERRUPT()` or `AsyncForeachResult::CANCEL()` respectively. When interrupted, a `forEach` task will not traverse the iterator anymore and notify interrupt listeners immediately. However, when cancelled, a `forEach` task will notify no listeners and immediately dispose the task away.

```
$handler->forEach(new ArrayIterator([1, 2, 3]))
->as(function(int $key, int $value) : AsyncForeachResult{
	echo $value;
	return $value === 2 ? AsyncForeachResult::INTERRUPT() : AsyncForeachResult::CONTINUE();
})
->onCompletion(function() : void{ echo "Completed"; })
->onInterruption(function() : void{ echo "Interrupted"; });
```

```
1
2
Interrupted

```

```
$handler->forEach(new ArrayIterator([1, 2, 3]))
->as(function(int $key, int $value) : AsyncForeachResult{
	echo $value;
	return AsyncForeachResult::CONTINUE();
})
->onCompletion(function() : void{ echo "Completed"; })
->onInterruption(function() : void{ echo "Interrupted"; });
```

```
1
2
3
Completed

```

---

Interruption of a `forEach` task can also occur externally (outside the handler) by calling the `interrupt()` method on the return value of `forEach`.

```
$foreach_task = $handler->forEach(new ArrayIterator([1, 2, 3]))
->as(function(int $key, int $value) : AsyncForeachResult{
	echo $value;
	return AsyncForeachResult::CONTINUE();
})
->onCompletion(function() : void{ echo "Completed"; })
->onInterruption(function() : void{ echo "Interrupted"; });

TaskScheduler::scheduleDelayedTask(function() use($foreach_task) : void{
	$foreach_task->interrupt();
}, ticks: 2);
```

```
1
2
Interrupted

```

---

A `forEach` task may be cancelled by calling the `cancel()` method on the return value of `forEach`, causing no interrupt or completion listeners to be notified. ```
$foreach_task = $handler->forEach(new ArrayIterator([1, 2, 3]))
->as(function(int $key, int $value) : AsyncForeachResult{
	echo $value;
	return AsyncForeachResult::CONTINUE();
})
->onCompletion(function() : void{ echo "Completed"; })
->onInterruption(function() : void{ echo "Interrupted"; });

TaskScheduler::scheduleDelayedTask(function() use($foreach_task) : void{
	$foreach_task->cancel();
}, ticks: 2);
```

```
1
2

```

---

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity30

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 95% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

852d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/247134f60bf8c5c7c8a2f06b0ecea431a052614283aac5093b57bde51039e34a?d=identicon)[muqsit](/maintainers/muqsit)

---

Top Contributors

[![Muqsit](https://avatars.githubusercontent.com/u/15074389?v=4)](https://github.com/Muqsit "Muqsit (19 commits)")[![poggit-bot](https://avatars.githubusercontent.com/u/22427965?v=4)](https://github.com/poggit-bot "poggit-bot (1 commits)")

---

Tags

api4asyncpm4pmmpvirion

### Embed Badge

![Health badge](/badges/muqsit-asynciterator/health.svg)

```
[![Health](https://phpackages.com/badges/muqsit-asynciterator/health.svg)](https://phpackages.com/packages/muqsit-asynciterator)
```

###  Alternatives

[muqsit/invmenu

A PocketMine-MP virion to create and manage virtual inventories!

2234.2k1](/packages/muqsit-invmenu)[muqsit/simple-packet-handler

Handle specific data packets (virion for PMMP API 4.0.0)

426.1k3](/packages/muqsit-simple-packet-handler)[dktapps/pmforms

Form API library for PocketMine-MP plugins

522.3k1](/packages/dktapps-pmforms)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
