Skip to content

Commit

Permalink
Added HOWTO use PromiseA with daemon
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandr-yulin committed May 16, 2019
1 parent 674bd19 commit 9f50f1f
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 14 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,16 @@ For more info check [example](/example) scripts.
echo $value . PHP_EOL;
});
```
> If use PromiseA promise with `daemon|cycle|loop` you must use `Swoole\Runtime::wait()`
```php
\Swoole\Runtime::enableCoroutine();
while (true) {
///
Some code with PromiseA
///
\Swoole\Runtime::wait();
}
```
[PHP image]: https://img.shields.io/badge/php-%3E%3D%207.2-blue.svg
[Swoole image]: https://img.shields.io/badge/swoole-%3E%3D%204.2-blue.svg
[Master branch]: https://github.com/streamcommon/promise/tree/master
Expand Down
3 changes: 2 additions & 1 deletion example/promise_true_async.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
declare(strict_types=1);

use Streamcommon\Promise\PromiseA;
use Swoole\Runtime;

if (PHP_SAPI !== 'cli' || !extension_loaded('swoole')) {
echo 'PromiseCo MUST running only in CLI mode with swoole extension' . PHP_EOL;
Expand All @@ -23,7 +24,7 @@
} else {
throw new \RuntimeException('File autoload.php not exists');
}
\Swoole\Runtime::enableCoroutine();
Runtime::enableCoroutine();

########### INIT ##############
$promise1 = PromiseA::create(function (callable $resolve) {
Expand Down
5 changes: 3 additions & 2 deletions example/promise_true_async_cycle.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
declare(strict_types=1);

use Streamcommon\Promise\PromiseA;
use Swoole\{Event, Runtime};

if (PHP_SAPI !== 'cli' || !extension_loaded('swoole')) {
echo 'PromiseCo MUST running only in CLI mode with swoole extension' . PHP_EOL;
Expand All @@ -23,7 +24,7 @@
} else {
throw new \RuntimeException('File autoload.php not exists');
}
\Swoole\Runtime::enableCoroutine();
Runtime::enableCoroutine();
########### INIT ##############
$promise1 = PromiseA::create(function (callable $resolve) {
$resolve(41);
Expand Down Expand Up @@ -63,5 +64,5 @@
});
########### RESULT ##############
$c++;
$promise1->wait(); // IF USE DAEMON MODE
Event::wait(); // IF USE DAEMON MODE
}
3 changes: 2 additions & 1 deletion example/subpromise_true_async.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
declare(strict_types=1);

use Streamcommon\Promise\PromiseA;
use Swoole\Runtime;

if (PHP_SAPI !== 'cli' || !extension_loaded('swoole')) {
echo 'PromiseCo MUST running only in CLI mode with swoole extension' . PHP_EOL;
Expand All @@ -23,7 +24,7 @@
} else {
throw new \RuntimeException('File autoload.php not exists');
}
\Swoole\Runtime::enableCoroutine();
Runtime::enableCoroutine();

#############################################################

Expand Down
9 changes: 0 additions & 9 deletions lib/PromiseA.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

use Swoole\Coroutine;
use Swoole\Coroutine\Channel;
use Swoole\Event;
use Throwable;

use function extension_loaded;
Expand Down Expand Up @@ -132,14 +131,6 @@ public function then(?callable $onFulfilled = null, ?callable $onRejected = null
return $promise;
}

/**
* Expect promise
*/
public function wait(): void
{
Event::wait();
}

/**
* Change promise state
*
Expand Down
1 change: 0 additions & 1 deletion test/PromiseATest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public function testPromise(): void
$promise3->then(function ($value) {
$this->assertNull($value);
});
$promise->wait();
}

/**
Expand Down

0 comments on commit 9f50f1f

Please sign in to comment.