本文整理匯總了PHP中React\Promise\Deferred::then方法的典型用法代碼示例。如果您正苦於以下問題:PHP Deferred::then方法的具體用法?PHP Deferred::then怎麽用?PHP Deferred::then使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類React\Promise\Deferred
的用法示例。
在下文中一共展示了Deferred::then方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: send
/**
* Transforms a message to the format requested by DaZeus and sends it
* @param mixed $data The data to be send, should be some stdObject or associative array
* @param boolean $callback Callback that receives the response, true for an
* empty callback, false if no callback is expected
* @return PromiseInterface
*/
public function send($data, $callback = true)
{
if (is_callable($callback) || $callback === true) {
$deferred = new Deferred();
$deferred->then(function ($message) use($callback) {
if (is_callable($callback)) {
return $callback($message);
}
return $message;
});
$this->watchers[] = $deferred->resolver();
$this->sendData($data);
return $deferred->promise();
} else {
$this->sendData($data);
return new FulfilledPromise();
}
}
示例2: handleConnectedSocks
public function handleConnectedSocks(Stream $stream, $host, $port, $timeout, $protocolVersion, $auth = null)
{
$deferred = new Deferred();
$resolver = $deferred->resolver();
$timerTimeout = $this->loop->addTimer($timeout, function () use($resolver) {
$resolver->reject(new Exception('Timeout while establishing socks session'));
});
if ($protocolVersion === '5' || $auth !== null) {
$promise = $this->handleSocks5($stream, $host, $port, $auth);
} else {
$promise = $this->handleSocks4($stream, $host, $port);
}
$promise->then(function () use($resolver, $stream) {
$resolver->resolve($stream);
}, function ($error) use($resolver) {
$resolver->reject(new Exception('Unable to communicate...', 0, $error));
});
$loop = $this->loop;
$deferred->then(function (Stream $stream) use($timerTimeout, $loop) {
$loop->cancelTimer($timerTimeout);
$stream->removeAllListeners('end');
return $stream;
}, function ($error) use($stream, $timerTimeout, $loop) {
$loop->cancelTimer($timerTimeout);
$stream->close();
return $error;
});
$stream->on('end', function (Stream $stream) use($resolver) {
$resolver->reject(new Exception('Premature end while establishing socks session'));
});
return $deferred->promise();
}