PHPackages                             amonite/async - 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. amonite/async

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

amonite/async
=============

PHP async, await and Promise classes, about to run async methods

33PHP

Since Feb 24Pushed 6y ago2 watchersCompare

[ Source](https://github.com/karkael64/php-async)[ Packagist](https://packagist.org/packages/amonite/async)[ RSS](/packages/amonite-async/feed)WikiDiscussions master Synced 4d ago

READMEChangelog (1)DependenciesVersions (1)Used By (0)

PHP Async methods
=================

[](#php-async-methods)

Asynchronous paradigm for PHP!
------------------------------

[](#asynchronous-paradigm-for-php)

It's now available! Just start your asynchronous environment with `await` function, then do whatever you want with functions and classes below!

Installation
------------

[](#installation)

You can :

1. run `composer require amonite/async`, or
2. copy `async.phar` file in your project directory and load it with a `require`.

Context
-------

[](#context)

As a JavaScript developer, I like the asynchronous paradigm. I decided to export this paradigm to allow PHP to take advantage of it: now, produce libraries that can process information while waiting for them to load data.

Non-blocking events
-------------------

[](#non-blocking-events)

In PHP, by default many functions stop script until the end. That's helpful with common arithmetic functions, but you wan also observe it when you request a data from your database (no matter your software), when you load a file from your computer or internet, when you use some socket, or run a command-line.

When your script wait for an other software, then your process is down. When a client start a request to the server, he waits for the previous client end of script. When thousands clients wait for a single client who waits for an only one heavy SQL request with 30+ seconds duration, then the server could crash as if it was a DDOS attack.

By using this library, I will develop a non-blocking HTTP server, then HTTPS, HTTP/2, SQL and filesystem reader/writer. Hope it will help!

Usage
=====

[](#usage)

1. [Understand await/async environment](#understand-awaitasync-environment)
    1. [Creating await environment](#creating-await-environment)
    2. [Triggering asynchronous events](#triggering-asynchronous-events)
    3. [Example: asynchronous socket](#example-asynchronous-socket)
2. [Use Promise class](#use-promise-class)
3. [Use Async class](#use-async-class)
4. [Use a combo of await and async](#use-a-combo-of-await-and-async)

Understand await/async environment
----------------------------------

[](#understand-awaitasync-environment)

Function `await` create an environment that allows to register an `async` event. An asynchronous event is an non-blocking function that continue script until end of a writing file function, a reading file function, connection start function and so on.

### Creating await environment

[](#creating-await-environment)

The function `$fn_env` in `await($fn_env) : mixed` create an asynchronous environment, and wait (many ticks) for all asynchronous events ends. The script is blocked in this function until the end of its async functions. If `$fn_env` returns a `Promise`, the `await` function returns its anwser resolved or throw its error rejected.

File wrapper `test/wrap.php` :

```
