我正在重构我的代码而且我遇到了一个问题:我想做这样的事情:
public function myFunc($time)
{
$this->otherFunc(function () {
try {
yield;
return true;
}
catch (RuntimeException $re) {
// Don't care
}
}, $time);
}
但是当我做的时候
foreach($this->myFunc(1000) as $action) {
// Code block here...
}
它引发了一个错误。
我认为问题在于匿名函数中的 yield 。有没有办法做到这一点?
我也尝试了Closure,但它不起作用:
public function myFunc($time)
{
$yield = yield;
$this->otherFunc(function($test) use ($yield) {
try {
$yield;
return true;
}
catch (RuntimeException $re) {
// Don't care
}
}, $time);
}
和......
foreach($this->myFunc(1000) as $action) {
{
// Code block here...
}
}
特点:
提前致谢!
更新:
Selenium Test的真实示例:
public function doWaitUntil($time)
{
$this->waitUntil(function($testCase) {
try {
yield($testCase);
return true;
}
catch (RuntimeException $re) {}
}, $time);
}
public function exampleTest()
{
foreach($this->doWaitUntil(2000) as $action) {
$action->byId('whatever')->click();
}
foreach($this->doWaitUntil(1000) as $action) {
$action->byId('whatever-dependent-element')->click();
}
// ...
}