PHP Classes

File: examples/1-simple.php

Recommend this page to a friend!
  Classes of Ahmad Mustapha   Simple PHP Promise Library   examples/1-simple.php   Download  
File: examples/1-simple.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: 4 years ago
Size: 541 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('Failed');
    }

    return
$deferred->promise();
}

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

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