Guzzle Promises follow Promises / A + . Therefore, we can rely on the official description to understand the behavior that interests you:
2.2.7.1. If either onFulfilled or onRejected returns x , run the promise resolution procedure [[Resolve]](promise2, x) .
2.2.7.2. If either onFulfilled or onRejected throws an exception e , promise2 must be rejected with e as the reason.
And later for case 2.2.7.2 :
2.3.2. If x is a promise, accept its state
Therefore, you can either execute the solution suggested by @lkoell or return RejectedPromise from the callback, which will cause the subsequent promise to accept the rejected state.
$promiseA = $promise ->then( function (ResponseInterface $res) { //ok }, function (RequestException $e) { //This will force $promiseB adopt $promiseC state and get rejected return $promiseC = new RejectedPromise($clientException); } ); $promiseB = $promiseA->then(null, function (ClientException $e) { // There you will get rejection });
This method is more flexible since you can reject a promise not only with an exception, but also for any reason (other than a promise).
source share