tickner/guzzle-to-react-promise

将 Guzzle Promise 转换为 React Promise

v1.0.0 2016-10-22 17:34 UTC

This package is not auto-updated.

Last update: 2024-09-28 19:20:42 UTC


README

一个函数,可以让你轻松地将 Guzzle Promise 转换为 React Promise

安装

composer require tickner/guzzle-to-react-promise

为什么?

React 和 Guzzle 使用不同的 Promise 实现,它们之间的互操作性不是很好。在一个使用 ReactPHP 的项目中,你可能需要使用 Guzzle 来处理 AWS SDK 的异步访问等问题。如果发生这种情况,当你收到一个 Guzzle 请求 Promise 时,你会发现你的 Promise 链意外地断开了。

使用本包提供的函数,你可以将这个 Promise 转换为你的应用程序更愿意使用的 React Promise。

示例

<?php

use GuzzleHttp\Promise\Promise as GuzzlePromise;
use function Tickner\GuzzleToReactPromise\guzzleToReactPromise;

$guzzlePromise = new GuzzlePromise(); // or a guzzle http request

$reactPromise = guzzleToReactPromise($guzzlePromise);

$reactPromise
    ->then(
        function($fulfilledValue) {
            echo $fulfilledValue;
        }
    )
;

$guzzlePromise->resolve('the val');

当 Guzzle Promise TaskQueue 运行时,Guzzle Promise 被解决,你的 React Promise 也会被解决并输出 the val

有一个简短的别名函数:g2rp

测试和贡献

要贡献,请克隆仓库并安装 composer 依赖项。

composer install

要测试,请在根目录中运行 phpunit。

vendor/bin/phpunit

许可证

MIT 许可证 的条款下发布。