title: How do I "extract" the data from a ReactPHP\Promise\Promise?
body: <p><em>Disclaimer: This is my first time working with <code>ReactPHP</code> and "php promises", so the solution might just be staring me in the face</em> </p>
  
  <p>I'm currently working on a little project where I need to create a Slack bot. I decided to use the <a href="https://botman.io/" rel="nofollow noreferrer">Botman</a> package and utilize it's Slack RTM driver. This driver utilizes ReactPHP's promises to communicate with the RTM API.</p>
  
  <p><strong>My problem:</strong></p>
  
  <p>When I make the bot reply on a command, I want to get the get retrieve the response from RTM API, so I can cache the ID of the posted message.</p>
  
  <p>Problem is that, the response is being returned inside one of these <code>ReactPHP\Promise\Promise</code> but I simply can't figure out how to retrieve the data.</p>
  
  <p><strong>What I'm doing:</strong></p>
  
  <p>So when a command is triggered, the bot sends a reply Slack:</p>
  
  <pre class="lang-php prettyprint-override"><code>$response = $bot-&gt;reply($response)-&gt;then(function (Payload $item) {
      return $this-&gt;reply = $item;
  });
  </code></pre>
  
  <p>But then <code>$response</code> consists of an (empty?) <code>ReactPHP\Promise\Promise</code>:</p>
  
  <pre class="lang-php prettyprint-override"><code>React\Promise\Promise^ {#887
    -canceller: null
    -result: null
    -handlers: []
    -progressHandlers: &amp; []
    -requiredCancelRequests: 0
    -cancelRequests: 0
  }
  </code></pre>
  
  <p>I've also tried using <code>done()</code> instead of <code>then()</code>, which is what (as far as I can understand) the <a href="https://github.com/reactphp/promise#done-vs-then" rel="nofollow noreferrer">official ReactPHP docs suggest you should use to retrieve data from a promise</a>:</p>
  
  <pre class="lang-php prettyprint-override"><code>$response = $bot-&gt;reply($response)-&gt;done(function (Payload $item) {
      return $this-&gt;reply = $item;
  });
  </code></pre>
  
  <p>But then <code>$response</code> returns as <code>null</code>.</p>
  
  <p>The funny thing is, during debugging, I tried to do a <code>var_dump($item)</code> inside the <code>then()</code> but had forgot to remove a non-existing method on the promise. But then the <code>var_dump</code> actually returned the data </p>
  
  <pre class="lang-php prettyprint-override"><code>$response = $bot-&gt;reply($response)-&gt;then(function (Payload $item) {
      var_dump($item);
      return $this-&gt;reply = $item;
  })-&gt;resolve();
  </code></pre>
  
  <p>So from what I can fathom, it's like I somehow need  to "execute" the promise again, even though it has been resolved before being returned.</p>
  
  <p>Inside the Bot's reply method, this is what's going on and how the ReactPHP promise is being generated:</p>
  
  <pre class="lang-php prettyprint-override"><code>public function apiCall($method, array $args = [], $multipart = false, $callDeferred = true)
  {
      // create the request url
      $requestUrl = self::BASE_URL . $method;
  
      // set the api token
      $args['token'] = $this-&gt;token;
  
      // send a post request with all arguments
      $requestType = $multipart ? 'multipart' : 'form_params';
      $requestData = $multipart ? $this-&gt;convertToMultipartArray($args) : $args;
  
      $promise = $this-&gt;httpClient-&gt;postAsync($requestUrl, [
          $requestType =&gt; $requestData,
      ]);
  
      // Add requests to the event loop to be handled at a later date.
      if ($callDeferred) {
          $this-&gt;loop-&gt;futureTick(function () use ($promise) {
              $promise-&gt;wait();
          });
      } else {
          $promise-&gt;wait();
      }
  
      // When the response has arrived, parse it and resolve. Note that our
      // promises aren't pretty; Guzzle promises are not compatible with React
      // promises, so the only Guzzle promises ever used die in here and it is
      // React from here on out.
      $deferred = new Deferred();
      $promise-&gt;then(function (ResponseInterface $response) use ($deferred) {
          // get the response as a json object
          $payload = Payload::fromJson((string) $response-&gt;getBody());
  
          // check if there was an error
          if (isset($payload['ok']) &amp;&amp; $payload['ok'] === true) {
              $deferred-&gt;resolve($payload);
          } else {
              // make a nice-looking error message and throw an exception
              $niceMessage = ucfirst(str_replace('_', ' ', $payload['error']));
              $deferred-&gt;reject(new ApiException($niceMessage));
          }
      });
  
      return $deferred-&gt;promise();
  }
  </code></pre>
  
  <p>You can see the full source of it <a href="https://github.com/mpociot/slack-client/blob/master/src/ApiClient.php#L407" rel="nofollow noreferrer">here</a>.</p>
  
  <p>Please just point me in some kind of direction. I feel like I tried everything, but obviously I'm missing something or doing something wrong.</p>