PHP Classes

File: examples/2-exception.php

Recommend this page to a friend!
  Classes of Ahmad Mustapha   Simple PHP Promise Library   examples/2-exception.php   Download  
File: examples/2-exception.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Simple PHP Promise Library
Register functions to call when conditions are met
Author: By
Last change:
Date: 3 years ago
Size: 592 bytes
 

Contents

Class file image Download
<?php
use SimplePromise\Deferred;

require
'vendor/autoload.php';

function
test($number)
{
   
$deferred = new Deferred();

    if (
$number > 2){
       
$deferred->resolve('Succeeded');
    }else{
       
$deferred->reject(new \Exception('Error occured'));
    }

    return
$deferred->promise();
}

test(1)->then(function ($data){
    echo
"First: {$data} \n";
})->
otherwise(function ($error){
    echo
"First: {$error->getMessage()} \n";
});

test(4)->then(function ($data){
    echo
"Second: {$data} \n";
})->
otherwise(function ($error){
    echo
"Second: {$error->getMessage()} \n";
});