SparkPost\SparkPostPromise::then PHP Method

then() public method

Hand off the response functions to the original promise and return a custom response or exception.
public then ( callable $onFulfilled = null, callable $onRejected = null )
$onFulfilled callable - function to be called if the promise is fulfilled
$onRejected callable - function to be called if the promise is rejected
    public function then(callable $onFulfilled = null, callable $onRejected = null)
    {
        return $this->promise->then(function ($response) use($onFulfilled) {
            if (isset($onFulfilled)) {
                $onFulfilled(new SparkPostResponse($response));
            }
        }, function ($exception) use($onRejected) {
            if (isset($onRejected)) {
                $onRejected(new SparkPostException($exception));
            }
        });
    }

Usage Example

 public function testUnsuccessfulAsyncRequestWithThen()
 {
     $responseBody = ['results' => 'failed'];
     $exceptionMock = Mockery::mock('Http\\Client\\Exception\\HttpException');
     $exceptionMock->shouldReceive('getResponse->getStatusCode')->andReturn(500);
     $exceptionMock->shouldReceive('getResponse->getBody->__toString')->andReturn(json_encode($responseBody));
     $guzzlePromise = new GuzzleRejectedPromise($exceptionMock);
     $promise = new SparkPostPromise(new GuzzleAdapterPromise($guzzlePromise, $this->resource->buildRequest('POST', 'transmissions', $this->postTransmissionPayload, [])));
     $promise->then(null, function ($exception) use($responseBody) {
         $this->assertEquals(500, $exception->getCode());
         $this->assertEquals($responseBody, $exception->getBody());
     })->wait();
 }