php-sparkpost icon indicating copy to clipboard operation
php-sparkpost copied to clipboard

SparkPostPromise is not chainable

Open andrews05 opened this issue 2 years ago • 0 comments

The SparkPostPromise::then function does not return the result of the callback functions. This means the new promise that it returns will have no data.

$result = $sparky->request(...)->then(function($response) {
  // do something
  return $response;
})->wait();
// $result is now null!

This is important if we want to add our own middleware to transform the response.

SparkPostPromise::then could be restructured like this to fix the issue (happy to open a PR if this is suitable):

    public function then(callable $onFulfilled = null, callable $onRejected = null)
    {
        $request = $this->request;

        return $this->promise->then(function ($response) use ($request) {
            return new SparkPostResponse($response, $request);
        }, function ($exception) use ($request) {
            return new SparkPostException($exception, $request)
        })->then($onFulfilled, $onRejected);
    }

andrews05 avatar Feb 13 '23 02:02 andrews05