carno-php/promise
Composer 安装命令:
composer require carno-php/promise
包简介
README 文档
README
Features
- Promises/A+ standards
- Addition commands e.g. race, all
- Simple and fast
Installation
composer require carno-php/promise
API & Usages
new Promise
Creates a promise with initialize executor
$promise = new Promise(static function (Promised $promise) { if (1) { $promise->resolve('success'); } else { $promise->reject('error'); } }); $promise->then(function () { echo 'promise has been resolved', PHP_EOL; }, function () { echo 'promise has been rejected', PHP_EOL; });
Promise::deferred
Creates a defer resolve promise
$promise = Promise::deferred(); $promise->then(function (string $var) { echo $var, PHP_EOL; }); $promise->resolve('var');
Promise::resolved
Creates a resolved promise
$promise = Promise::resolved('var'); $promise->then(function (string $var) { echo $var, PHP_EOL; });
Promise::rejected
Creates a rejected promise
$promise = Promise::rejected(new Exception('Test')); $promise->catch(function (Throwable $e) { echo 'failure with ', $e->getMessage(), PHP_EOL; });
Promise::all
Returns a single promise that resolves when all of the promises in the argument have resolved or when the argument contains no promises. It rejects with the reason of the first promise that rejects.
$p1 = Promise::deferred(); $p2 = Promise::deferred(); $pa = Promise::all($p1, $p2); $pa->then(function (array $results) { echo 'all resovled with results ', var_export($results, true), PHP_EOL; }); $p1->resolve('var1'); $p2->resolve('var2');
Promise::race
Returns a promise that resolves or rejects as soon as one of the promises in the argument resolves or rejects, with the value or reason from that promise.
$p1 = Promise::deferred(); $p1->then(function () { echo 'p1 has been resolved', PHP_EOL; }, function () { echo 'p1 has been rejected', PHP_EOL; }); $p2 = Promise::deferred(); $p2->then(function () { echo 'p2 has been resolved', PHP_EOL; }, function () { echo 'p2 has been rejected', PHP_EOL; }); $pr = Promise::race($p1, $p2); $pr->then(function (string $var) { echo 'race result is ', $var, PHP_EOL; }); $p1->resolve('test'); // or $p2->reject();
Promise->pended
Check that promise is neither fulfilled and rejected
$promise = Promise::deferred(); echo '#1 promise is pended ? ', $promise->pended() ? 'yes' : 'no', PHP_EOL; $promise->resolve(); echo '#2 promise is pended ? ', $promise->pended() ? 'yes' : 'no', PHP_EOL;
Promise->chained
Check that promise has more chained promises (connected with then)
$promise = Promise::deferred(); echo '#1 promise is chained ? ', $promise->chained() ? 'yes' : 'no', PHP_EOL; $promise->then(function () { }); echo '#2 promise is chained ? ', $promise->chained() ? 'yes' : 'no', PHP_EOL;
Promise->sync
Make promise synced with other promise (resolves and rejects)
$next = Promise::deferred(); $next->then(function (string $var) { echo 'NEXT promise been resolved with ', $var, PHP_EOL; }, function (string $var) { echo 'NEXT promise been rejected with ', $var, PHP_EOL; }); $promise = Promise::deferred()->sync($next); $promise->then(function (string $var) { echo 'CURRENT promise been resolved with ', $var, PHP_EOL; }, function (string $var) { echo 'CURRENT promise been rejected with ', $var, PHP_EOL; }); $promise->resolve('hello'); // or $promise->reject('world');
Promise->fusion
Set promise to throws exception if rejected with an error, otherwise exception will only as promise's result
Promise::deferred()->fusion()->throw(new Exception('test'));
Promise->then
$promise = Promise::deferred(); $promise->then(function (...$args) { echo 'promise resolved with args ', var_export($args, true), PHP_EOL; }, function (...$args) { echo 'promise rejected with args ', var_export($args, true), PHP_EOL; }); $promise->resolve('hello', 'world'); // or $promise->reject('hello', 'world');
Promise->catch
Alias of Promise->then(null, onRejects)
Promise->resolve
Resolves a promise
Promise::deferred()->resolve('var1', 'var2');
Promise->reject
Rejects a promise
Promise::deferred()->reject('var1', 'var2');
Promise->throw
Alias of Promise->reject(exception)
Benchmark
php benchmark.php
output
cost 362 ms | op 1.105 μs
carno-php/promise 适用场景与选型建议
carno-php/promise 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.17k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2018 年 08 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 carno-php/promise 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 carno-php/promise 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.17k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 5
- 依赖项目数: 26
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-08-15