public function __construct(Promise $promise) { $this->valids[$this->readPos] = new Deferred(); $promise->watch(function ($data) { $curPos = $this->writePos; $this->writePos++; $this->valids[] = new Deferred(); $this->values[$curPos] = $data; $this->valids[$curPos]->succeed(true); }); $promise->when(function ($error, $result) { if ($error) { $this->valids[$this->writePos]->fail($error); } else { $curPos = $this->writePos; $this->values[$this->writePos++] = $result; $this->valids[$curPos]->succeed(false); } }); }