mirror of
https://github.com/danog/amp.git
synced 2024-11-26 20:15:00 +01:00
Normalize coroutine() return to Promise type
This commit is contained in:
parent
ad4831888f
commit
5cad406795
@ -734,7 +734,13 @@ function wait(Promise $promise) {
|
||||
function coroutine(callable $func) {
|
||||
return function () use ($func) {
|
||||
$out = \call_user_func_array($func, \func_get_args());
|
||||
return ($out instanceof \Generator) ? resolve($out) : $out;
|
||||
if ($out instanceof \Generator) {
|
||||
return resolve($out);
|
||||
} elseif ($out instanceof Promise) {
|
||||
return $out;
|
||||
} else {
|
||||
return new Success($out);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -671,6 +671,30 @@ class FunctionsTest extends \PHPUnit_Framework_TestCase {
|
||||
$this->assertSame(1, $invoked);
|
||||
}
|
||||
|
||||
public function testCoroutineWrapsNonGeneratorReturnInPromise() {
|
||||
$co = \Amp\coroutine(function () {
|
||||
return 42;
|
||||
});
|
||||
$out = $co();
|
||||
$this->assertInstanceOf("\Amp\Success", $out);
|
||||
$invoked = false;
|
||||
$out->when(function ($error, $result) use (&$invoked) {
|
||||
$this->assertNull($error);
|
||||
$this->assertSame(42, $result);
|
||||
$invoked = true;
|
||||
});
|
||||
$this->assertTrue($invoked);
|
||||
}
|
||||
|
||||
public function testCoroutineReturnsPromiseResultUnmodified() {
|
||||
$success = new \Amp\Success;
|
||||
$co = \Amp\coroutine(function () use ($success) {
|
||||
return $success;
|
||||
});
|
||||
$out = $co();
|
||||
$this->assertSame($success, $out);
|
||||
}
|
||||
|
||||
public function testNestedCoroutineResolutionContinuation() {
|
||||
$invoked = 0;
|
||||
\Amp\run(function () use (&$invoked) {
|
||||
|
Loading…
Reference in New Issue
Block a user