Skip to content

Instantly share code, notes, and snippets.

@IlyaZha
Created June 18, 2020 12:44
Show Gist options
  • Save IlyaZha/7c2ca9d95bf41a6292bdfad7168763f5 to your computer and use it in GitHub Desktop.
Save IlyaZha/7c2ca9d95bf41a6292bdfad7168763f5 to your computer and use it in GitHub Desktop.
<?php
use GuzzleHttp\Promise\Promise;
use GuzzleHttp\Promise\PromiseInterface;
/**
* Guzzle promise, which can get few params to functions and return few values.
*
* Class PromisePlus
*/
class PromisePlus implements PromiseInterface
{
/**
* @var Promise
*/
private $promise;
/**
* PromisePlus constructor.
*/
public function __construct()
{
$this->promise = new Promise();
}
/**
* @param callable|null $onFulfilled
* @param callable|null $onRejected
* @return \GuzzleHttp\Promise\PromiseInterface|void
*/
public function then(callable $onFulfilled = null, callable $onRejected = null)
{
$funcFulfilled = function ($value) use ($onFulfilled) {
return $onFulfilled(...$value);
};
$funcRejected = function ($value) use ($onRejected) {
return $onRejected(...$value);
};
$this->promise->then($funcFulfilled, $funcRejected);
}
/**
* @param callable $onRejected
* @return \GuzzleHttp\Promise\PromiseInterface|void
*/
public function otherwise(callable $onRejected)
{
$funcRejected = function ($value) use ($onRejected) {
return $onRejected(...$value);
};
$this->promise->otherwise($funcRejected);
}
/**
* @param mixed $value
*/
public function resolve($value)
{
$this->promise->resolve(func_get_args());
}
/**
* @param mixed $reason
*/
public function reject($reason)
{
$this->promise->reject(func_get_args());
}
public function getState()
{
$this->promise->getState();
}
public function cancel()
{
$this->promise->cancel();
}
public function wait($unwrap = true)
{
$this->promise->wait($unwrap);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment