onion/promise
Composer 安装命令:
composer require onion/promise
包简介
README 文档
README
This is an Promises/A+ implementation that should be fully compatible with any already existing implementations without any 3rd party dependencies.
This package defines 2 interfaces PromiseInterface & ThenableInterface as per the spec also there are some helper functions as well as some that provide a more
sugary feel to the code.
is_thenable - Function that checks if an object is "thenable"
coroutine - Push a task as a promise. This one changes it's implementation depending on whether or not the swoole extension is
available on the server. If it is then the native coroutines available as go function are used, alternatively it falls back to an approach similar to an event loop.
A tick function is registered using register_tick_function that will invoke 1 task per so that not to bring your application to a complete halt when executing. As well as there is a function registered for shutdown that will run all remaining tasks in a blocking way until the queue is empty - albeit hanging the request.
For when the synchronous mode is used, you should do declare(ticks=1) see the PHP Docs
NOTE: Please, keep in mind that your code will block until the task is processed so this 'mode' is not recommended and users should look at alternative approaches for their heavy tasks or install swoole
async - alias for coroutine, intended to mimic the async keyword in JS/TS/C#
==============
Usage
use \Onion\Framework\Promise\async; $promise = async(function () use ($orm, $id, $password) { $user = $orm->findById($id); // if it throws any exception the promise will immediately get rejected if (!$user->isActive()) { throw new \RuntimeException('User is not activated yet'); } if (!password_verify($password, $user->getPassword())) { throw new \InvalidArgumentException('Invalid password provided'); } return $user; })->then(function (User $user) use ($paymentService) { $paymentService->processUserPayment($user->getId()); echo "User {$user->getId()} processed successfully"; }, function (\Throwable $ex) { echo "Ops.. {$ex->getMessage()}"; })->otherwise(function (\Throwable $ex) { // Do something else with the exception maybe ? })->finally(function () use ($orm) { // Regardless of the state change, but it is always called at the end of execution $orm->disconnect(); });
The function passed to async/coroutine will be executed immediately so do keep in mind that
when using without swoole.
onion/promise 适用场景与选型建议
onion/promise 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 309 次下载、GitHub Stars 达 2, 最近一次更新时间为 2019 年 01 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 onion/promise 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 onion/promise 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 309
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 4
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-01-21