php-etl / promise
用于ETL操作的Promise实现。
0.1.0
2019-10-28 08:14 UTC
Requires
- php: ^7.2
Requires (Dev)
- ext-xdebug: *
- drupol/phpspec-code-coverage: ^4.0@dev
- infection/infection: dev-master
- phpspec/phpspec: ^5.1
- phpstan/phpstan: ^0.12.0@dev
This package is auto-updated.
Last update: 2024-08-30 01:08:09 UTC
README
当你需要组织在某个任务必须稍后执行时的回调时,Promise模式非常有用。
假设我们有一个带有doSomethingAsync
方法的类,该方法将稍后产生对onSuccess
事件处理器的调用。
<?php use Kiboko\Component\ETL\Promise\DeferredInterface; use Kiboko\Component\ETL\Promise\Promise; use Kiboko\Component\ETL\Promise\PromiseInterface; class SomeEvent { public $value; } class AsyncTask { /** @var PromiseInterface */ private $promise; public function doSomethingAsync(): DeferredInterface { // Do something $this->promise = new Promise(); return $this->promise->defer(); } public function onSuccess(SomeEvent $event) { $this->promise->resolve($event->value); } }
然后你可以注册以下内容
<?php $task = new AsyncTask(); $task ->doSomethingAsync() ->then( function(string $value) { echo $value; return $value; } );