PHP code example of onion / promise

1. Go to this page and download the library: Download onion/promise library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

onion / promise example snippets


use \Onion\Framework\Promise\async;

$promise = async(function () use ($orm, $id, $password) {
    $user = $orm->findById($id); // if it throws any exception the promise will immediately get rejected

    if (!$user->isActive()) {
        throw new \RuntimeException('User is not activated yet');
    }

    if (!password_verify($password, $user->getPassword())) {
        throw new \InvalidArgumentException('Invalid password provided');
    }

    return $user;
})->then(function (User $user) use ($paymentService) {
    $paymentService->processUserPayment($user->getId());
    echo "User {$user->getId()} processed successfully";
}, function (\Throwable $ex) {
    echo "Ops.. {$ex->getMessage()}";
})->otherwise(function (\Throwable $ex) {
    // Do something else with the exception maybe ?
})->finally(function () use ($orm) {
    // Regardless of the state change, but it is always called at the end of execution
    $orm->disconnect();
});