承接 logicalsteps/async 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

logicalsteps/async

最新稳定版本:2.0.0

Composer 安装命令:

composer require logicalsteps/async

包简介

async await implementation using generator function in php

README 文档

README

Build Status Code Coverage Scrutinizer Code Quality Latest Stable Version Total Downloads License

Simplify your asynchronous code and make it readable like synchronous code. It works similar to Async and await in other languages such as JavaScript and C#

It can be used standalone with callback functions. It also works with the promise interface of the following frameworks

It can do cooperative multitasking with the event loop interfaces of the following frameworks

Installation

Async can be installed with Composer by adding it as a dependency to your project's composer.json file. It can be done using the following command.

composer require logicalsteps/async

Please refer to Composer's documentation for more detailed installation and usage instructions.

Usage

Consider the following example

<?php
require __DIR__ . '/../vendor/autoload.php';
use Web3\Web3; //installed with `composer require sc0vu/web3.php` on the commandline

function balance($accountNumber)
{
    $web3 = new Web3('http://localhost:8545');
    $eth = $web3->eth;
    $eth->accounts(function ($error, $result) use ($eth, $accountNumber) {
        if ($error) {
            return;
        }
        $accounts = $result;
        $eth->getBalance($accounts[$accountNumber], function ($error, $result) {
            if ($error) {
                return;
            }
            var_export((int)$result->value);
        });
    });
}

balance(0);

If it is all synchronous, our function will simply be

function balance($accountNumber)
{
    $web3 = new Web3('http://localhost:8545');
    $eth = $web3->eth;
    $accounts = $eth->accounts();
    $balance = $eth->getBalance($accounts[$accountNumber]);
    return (int)$balance->value;
}

var_export(balance(0));

With Async library it can be written as the following

use LogicalSteps\Async\Async;
use Web3\Web3;

function balance($accountNumber)
{

    $web3 = new Web3('http://localhost:8545');
    $eth = $web3->eth;
    $accounts = yield [$eth, 'accounts'];
    $balance = yield [$eth, 'getBalance', $accounts[$accountNumber]];
    $value = (int)$balance->value;
    return $value;
}

Async::await(balance(0))->then('var_export');

Now the code is clean and looks synchronous, but runs asynchronously for better performance getting us best of both worlds :)

For more examples and integration with the frameworks take a look at examples folder

统计信息

  • 总下载量: 1.07k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 0
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 2
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-09-29