Skip to content

Provides a wrapper class for Promise with Generator exporting and register Throwable handlers

License

Notifications You must be signed in to change notification settings

presentkim-pm/awaitable-promise

Repository files navigation

Poggit CI Stars License


Logo

awaitable-promise

Provides a wrapper class for Promise with Generator exporting and register Throwable handlers!

View in Poggit · Report a bug · Request a feature

About The Project

✔️ Provides a wrapper class for Promise with Generator exporting and register Throwable handlers!

  • kim\present\awaitablepromise\AwaitablePromise

Installation

See Official Poggit Virion Documentation


How to use?

1-1. Create AwaitablePromise from Generator

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());
    }
}

1-2. create AwaitablePromise from callable that returns Generator

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();
        });
    }
}

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.

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

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

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

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

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;
    }
}

License

Distributed under the MIT. See LICENSE for more information

About

Provides a wrapper class for Promise with Generator exporting and register Throwable handlers

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages