React\Promise\Deferred::promise PHP Method

promise() public method

public promise ( )
    public function promise()
    {
        if (null === $this->promise) {
            $this->promise = new Promise(function ($resolve, $reject) {
                $this->resolveCallback = $resolve;
                $this->rejectCallback = $reject;
            }, $this->canceller);
        }
        return $this->promise;
    }

Usage Example

Example #1
0
 public function run()
 {
     $this->deferred = new Deferred();
     $this->deferred->promise()->progress(function ($event) {
         $this->data[$event['part']] = $event['data'];
         unset($this->unsettled[$event['part']]);
         if ($this->isEverythingHasBeenReceived()) {
             $this->deferred->resolve();
         }
     })->then(function () {
         if (isset($this->cancel_timer)) {
             $this->loop->cancelTimer($this->cancel_timer);
             unset($this->cancel_timer);
         }
     })->done(function () {
         $response = call_user_func($this->then_callback, $this->data);
         $headers = ['Content-Type' => 'text/plain'];
         $this->response->writeHead(200, $headers);
         $this->response->end($response);
     }, function () {
         $headers = ['Content-Type' => 'text/plain'];
         $this->response->writeHead(404, $headers);
         $this->response->end("Failed");
     });
     if (empty($this->requests)) {
         $this->deferred->resolve();
     } else {
         $this->registerCancelTimer();
         foreach ($this->requests as $request) {
             $request->end();
         }
     }
     return $this;
 }
All Usage Examples Of React\Promise\Deferred::promise