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;
}