Inheritance: extends Pool
Ejemplo n.º 1
0
    trait ErrorDetector
    {
        public function onError(Promisable $promised)
        {
            printf("Oh noes: %s\n", (string) $promised->getError());
        }
    }
    class AddTwo extends Thenable
    {
        use ErrorDetector;
        public function onFulfilled(Promisable $promised)
        {
            $promised->meaning += 2;
            /* throwing an exception here will cause invocation of 
            			PrintMeaning::onError */
        }
    }
    class PrintMeaning extends Thenable
    {
        use ErrorDetector;
        public function onFulfilled(Promisable $promised)
        {
            printf("The meaning of life + 2: %d\n", $promised->meaning);
            /* You can access exceptions thrown here using $promised->getError */
        }
    }
    $manager = new PromiseManager();
    $promise = new Promise($manager, new CalculateTheMeaningOfLife());
    $promise->then(new AddTwo($promise))->then(new PrintMeaning($promise));
    $manager->shutdown();
}
Ejemplo n.º 2
0
             throw new \RuntimeException("failed to load HTML at {$promised->url}");
         }
     }
 }
 class DOMPrinter extends Thenable
 {
     use DOMError;
     public function onFulfilled(Promisable $promised)
     {
         $promised->getLogger()->log("%s: %s\n", $promised->getName(), $promised->getDescription());
         /* allow the object to be collected */
         $promised->setGarbage(true);
     }
 }
 /* start a pool of 8 threads to fulfill promises */
 $manager = new PromiseManager(8);
 /* get 100 random internal function names */
 $functions = get_defined_functions();
 foreach (array_rand($functions["internal"], 100) as $function) {
     $names[] = $functions["internal"][$function];
 }
 $functions = $names;
 /* create a logger for threads in the pool to share */
 $logger = new SafeLogger();
 /* create promises */
 foreach ($functions as $index => $function) {
     $functions[$index] = new Promise($manager, new DOMFetcher($logger, $function));
     $functions[$index]->then(new DOMParser($functions[$index]))->then(new DOMPrinter($functions[$index]));
 }
 /* begin to collect ... */
 while ($manager->hasWork()) {