✔️ Provides a wrapper class for Promise with Generator exporting and register Throwable handlers!
kim\present\awaitablepromise\AwaitablePromise
See Official Poggit Virion Documentation
Use AwaitablePromise::g2p(\Generator $generator) : AwaitablePromise
use kim\present\awaitablepromise\AwaitablePromise as Promise;
class Test {
public function example() : \Generator{
return yield from Something::getAsync();
}
public function getSomething() : Promise{
return Promise::g2p(example());
}
}
Use AwaitablePromise::f2p(callable $callable) : AwaitablePromise
use kim\present\awaitablepromise\AwaitablePromise as Promise;
class Test {
public function getSomething() : Promise{
return Promise::f2p(function() : \Generator{
return yield from Something::getAsync();
});
}
}
Use AwaitablePromise::r2p($result) : AwaitablePromise
It for synchronous operation or cached result that you want to wrap with AwaitablePromise.
use kim\present\awaitablepromise\AwaitablePromise as Promise;
class Test {
public function getSomething() : Promise{
return Promise::r2p(Something::getSync());
}
}
Use AwaitablePromise::then(callable $func) : AwaitablePromise
use kim\present\awaitablepromise\AwaitablePromise as Promise;
class Test {
public function getSomething() : void{
Promise::r2p(Something::getSync())->then(function($result){
echo "SUCCESS: " . $result . PHP_EOL;
});
}
}
Use AwaitablePromise::catch(callable $func) : AwaitablePromise
use kim\present\awaitablepromise\AwaitablePromise as Promise;
class Test {
public function getSomething() : void{
Promise::r2p(Something::getSync())->catch(function(\Throwable $throwable){
echo "ERROR: " . $throwable->getMessage() . PHP_EOL;
});
}
}
Use AwaitablePromise::finally(callable $func) : AwaitablePromise
use kim\present\awaitablepromise\AwaitablePromise as Promise;
class Test {
public function getSomething() : void{
Promise::r2p(Something::getSync())->finally(function(){
echo "FINALLY" . PHP_EOL;
});
}
}
Use AwaitablePromise::getRealPromise() : \pocketmine\promise\Promise
use kim\present\awaitablepromise\AwaitablePromise as Promise;
class Test {
public function getSomething() : void{
Promise::r2p(Something::getSync())->getRealPromise()->onCompletion(
function($result){ echo "SUCCESS: " . $result . PHP_EOL; },
function(\Throwable $throwable){ echo "ERROR: " . $throwable->getMessage() . PHP_EOL; }
);
}
}
4. Using with await-generator
Use AwaitablePromise::await() : \Generator
use kim\present\awaitablepromise\AwaitablePromise as Promise;
class Test {
public function getSomething() : \Generator{
$promise = Promise::r2p(Something::getSync());
$result = yield from $promise->await();
echo "SUCCESS: " . $result . PHP_EOL;
}
}
Distributed under the MIT. See LICENSE for more information