carno-php/promise

1.0.1 2019-07-26 02:28 UTC

This package is auto-updated.

Last update: 2024-08-29 03:31:37 UTC


README

特性

  • Promises/A+ 标准
  • 添加命令,例如 race, all
  • 简单快速

安装

composer require carno-php/promise

API & 使用

new Promise

创建一个具有初始化执行器的 Promise

$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

创建一个延迟解析的 Promise

$promise = Promise::deferred();
$promise->then(function (string $var) {
    echo $var, PHP_EOL;
});
$promise->resolve('var');

Promise::resolved

创建一个已解析的 Promise

$promise = Promise::resolved('var');
$promise->then(function (string $var) {
    echo $var, PHP_EOL;
});

Promise::rejected

创建一个已拒绝的 Promise

$promise = Promise::rejected(new Exception('Test'));
$promise->catch(function (Throwable $e) {
    echo 'failure with ', $e->getMessage(), PHP_EOL;
});

Promise::all

返回一个单独的 Promise,当所有传入的 Promise 都已解析或当参数不包含任何 Promise 时解析。它使用第一个拒绝的 Promise 的原因拒绝。

$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

返回一个 Promise,当传入的 Promise 中的任何一个解析或拒绝时解析或拒绝,并使用该 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

检查 Promise 是否既未满足也未拒绝

$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

检查 Promise 是否有更多的链式 Promise(通过 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

使 Promise 与其他 Promise 同步(解析和拒绝)

$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

设置 Promise,如果拒绝则抛出异常,否则异常将仅作为 Promise 的结果

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

Promise->then 的别名,参数为 onRejects

Promise->resolve

解析一个 Promise

Promise::deferred()->resolve('var1', 'var2');

Promise->reject

拒绝一个 Promise

Promise::deferred()->reject('var1', 'var2');

Promise->throw

Promise->reject 的别名

基准测试

php benchmark.php

输出

cost 362 ms | op 1.105 μs