Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/presentkim-pm/awaitable-promise
Provides a wrapper class for Promise with Generator exporting and register Throwable handlers
https://github.com/presentkim-pm/awaitable-promise
Last synced: about 2 months ago
JSON representation
Provides a wrapper class for Promise with Generator exporting and register Throwable handlers
- Host: GitHub
- URL: https://github.com/presentkim-pm/awaitable-promise
- Owner: presentkim-pm
- License: mit
- Created: 2024-06-02T10:43:21.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2024-06-02T10:47:03.000Z (7 months ago)
- Last Synced: 2024-06-02T11:57:44.433Z (7 months ago)
- Language: PHP
- Size: 8.79 KB
- Stars: 2
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![Poggit CI][poggit-ci-badge]][poggit-ci-url]
[![Stars][stars-badge]][stars-url]
[![License][license-badge]][license-url]
awaitable-promise
Provides a wrapper class for Promise with Generator exporting and register Throwable handlers![View in Poggit][poggit-ci-url] · [Report a bug][issues-url] · [Request a feature][issues-url]
## About The Project
:heavy_check_mark: Provides a wrapper class for Promise with Generator exporting and register Throwable handlers!
- `kim\present\awaitablepromise\AwaitablePromise`
-----
## Installation
See [Official Poggit Virion Documentation](https://github.com/poggit/support/blob/master/virion.md)
-----
## How to use?
### 1-1. Create `AwaitablePromise` from `Generator`
Use `AwaitablePromise::g2p(\Generator $generator) : AwaitablePromise`
```php
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());
}
}
```
### 1-2. create `AwaitablePromise` from `callable` that returns `Generator`
Use `AwaitablePromise::f2p(callable $callable) : AwaitablePromise`
```php
use kim\present\awaitablepromise\AwaitablePromise as Promise;class Test {
public function getSomething() : Promise{
return Promise::f2p(function() : \Generator{
return yield from Something::getAsync();
});
}
}
```
### 1-3. create `AwaitablePromise` from result
Use `AwaitablePromise::r2p($result) : AwaitablePromise`
It for synchronous operation or cached result that you want to wrap with AwaitablePromise.```php
use kim\present\awaitablepromise\AwaitablePromise as Promise;class Test {
public function getSomething() : Promise{
return Promise::r2p(Something::getSync());
}
}
```
### 2-1. Register a resolve handler (handle result
Use `AwaitablePromise::then(callable $func) : AwaitablePromise`
```php
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;
});
}
}
```
### 2-2. Register a reject handler (handle throwable)
Use `AwaitablePromise::catch(callable $func) : AwaitablePromise`
```php
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;
});
}
}
```
### 2-3. Register a finally handler (handle both result and throwable)
Use `AwaitablePromise::finally(callable $func) : AwaitablePromise`
```php
use kim\present\awaitablepromise\AwaitablePromise as Promise;class Test {
public function getSomething() : void{
Promise::r2p(Something::getSync())->finally(function(){
echo "FINALLY" . PHP_EOL;
});
}
}
```
### 3. Get real Promise object (`pocketmine\promise\Promise`)
Use `AwaitablePromise::getRealPromise() : \pocketmine\promise\Promise`
```php
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`](https://github.com/SOF3/await-generator)
Use `AwaitablePromise::await() : \Generator`
```php
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;
}
}
```-----
## License
Distributed under the **MIT**. See [LICENSE][license-url] for more information
[poggit-ci-badge]: https://poggit.pmmp.io/ci.shield/presentkim-pm/awaitable-promise/awaitable-promise?style=for-the-badge
[stars-badge]: https://img.shields.io/github/stars/presentkim-pm/awaitable-promise.svg?style=for-the-badge
[license-badge]: https://img.shields.io/github/license/presentkim-pm/awaitable-promise.svg?style=for-the-badge
[poggit-ci-url]: https://poggit.pmmp.io/ci/presentkim-pm/awaitable-promise/awaitable-promise
[stars-url]: https://github.com/presentkim-pm/awaitable-promise/stargazers
[issues-url]: https://github.com/presentkim-pm/awaitable-promise/issues
[license-url]: https://github.com/presentkim-pm/awaitable-promise/blob/main/LICENSE
[project-icon]: https://raw.githubusercontent.com/presentkim-pm/awaitable-promise/main/assets/icon.png