PHP code example of libelulasoft / yii2-async-await

1. Go to this page and download the library: Download libelulasoft/yii2-async-await library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

libelulasoft / yii2-async-await example snippets


return [
    'id' => 'app-async',
    'basePath' => dirname(__DIR__),
    // Your controllers, you can change this to backend\controllers
    'controllerNamespace' => 'frontend\controllers',
    'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
    'aliases' => [
        '@bower' => '@vendor/bower-asset',
        '@npm'   => '@vendor/npm-asset',
    ],
    // Required components for async functions 
    'components' => [
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        // Config your database 
        'db' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'mysql:host=localhost;dbname=yii2advanced',
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8',
        ],
    ],
    'params' => [],
];

defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
// Autoload for composer an yii2 
figuration for async 
$config = ;
 
[
    ...,
    'components' => [
        // If you want to use callbacks 
        'asyncAwait' => [
            'class' => \Libelulasoft\AsyncAwait\AsyncAwait::class,
            // Your own entry script, see the above examples
            'loader' => __DIR__ . '/async.php'
        ],
        // If you want to use classes, this is more faster 
        'asyncTask' => [
            'class' => \Libelulasoft\AsyncAwait\AsyncTask::class,
            // Your own entry script, see the above examples
            'loader' => __DIR__ . '/async.php'
        ],
    ]
]
 

use common\models\User;

// Adding you async function 
Yii::$app->asyncAwait->add('sendUserEmail', function (string $idUser, string $sender) {
    $user = User::findOne($idUser);
    // Return any serializable data, is prefer return a basic array response 
    return \common\models\Email::sendUser($user, $sender);
}, $idUser, $sender);

Yii::$app->asyncAwait->add('sendUserMessage', function (string $message, string $number) {
    if ($number === '') return 'Number is 

// This class is autoloadable 
namespace common\tasks;

use Amp\Parallel\Worker\Environment;
use Amp\Parallel\Worker\Task;
use yii\helpers\VarDumper;

class PrintTask implements Task
{
    private $text;

    public function __construct(string $text)
    {
        $this->text = $text;
    }

    /**
     * {@inheritdoc}
     */
    public function run(Environment $environment)
    {
        return [
            'response' => "FUTURE PROMISE WORKER {$this->text}",
            'enviroment' => VarDumper::dumpAsString($environment),
        ];
    }
}


/** @var \Libelulasoft\AsyncAwait\AsyncTask */
$async = Yii::$app->asyncTask;

$async->add('1', new PrintTask('THIS IS MY LARGE TEXT'));
$response = $async->run();