Skip to content

Commit

Permalink
Merge pull request guzzle#95 from ikeyan/patch-1
Browse files Browse the repository at this point in the history
Make Promise.php shorter and more readable
  • Loading branch information
Tobion authored Feb 15, 2020
2 parents 6379353 + 1df6046 commit 89b1a76
Showing 1 changed file with 14 additions and 23 deletions.
37 changes: 14 additions & 23 deletions src/Promise.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ public function then(

// Return a fulfilled promise and immediately invoke any callbacks.
if ($this->state === self::FULFILLED) {
return $onFulfilled
? promise_for($this->result)->then($onFulfilled)
: promise_for($this->result);
$promise = promise_for($this->result);
return $onFulfilled ? $promise->then($onFulfilled) : $promise;
}

// It's either cancelled or rejected, so return a rejected promise
Expand All @@ -61,19 +60,15 @@ public function wait($unwrap = true)
{
$this->waitIfPending();

$inner = $this->result instanceof PromiseInterface
? $this->result->wait($unwrap)
: $this->result;

if ($this->result instanceof PromiseInterface) {
return $this->result->wait($unwrap);
}
if ($unwrap) {
if ($this->result instanceof PromiseInterface
|| $this->state === self::FULFILLED
) {
return $inner;
} else {
// It's rejected so "unwrap" and throw an exception.
throw exception_for($inner);
if ($this->state === self::FULFILLED) {
return $this->result;
}
// It's rejected so "unwrap" and throw an exception.
throw exception_for($this->result);
}
}

Expand Down Expand Up @@ -263,17 +258,13 @@ private function invokeWaitList()
$this->waitList = null;

foreach ($waitList as $result) {
while (true) {
do {
$result->waitIfPending();
$result = $result->result;
} while ($result instanceof Promise);

if ($result->result instanceof Promise) {
$result = $result->result;
} else {
if ($result->result instanceof PromiseInterface) {
$result->result->wait(false);
}
break;
}
if ($result instanceof PromiseInterface) {
$result->wait(false);
}
}
}
Expand Down

0 comments on commit 89b1a76

Please sign in to comment.