<?php require_once "../vendor/autoload.php"; use Hprose\Swoole\Server; use Hprose\Future; $server = new Server("http://0.0.0.0:1315"); $server->setErrorTypes(E_ALL); $server->setDebugEnabled(); $server->addFunction(function ($a, $b) use($server) { $promise = new Future(); swoole_timer_after(1000, function () use($a, $b, $promise) { $promise->resolve($a + $b); }); return $promise; }, "sum"); $server->start();
function settle($array) { return toFuture($array)->then(function ($array) { $keys = array_keys($array); $n = count($array); $result = array(); if ($n === 0) { return value($result); } $future = new Future(); $oncomplete = function ($index, $f) use($future, &$result, &$n, $keys) { return function () use($index, $f, $future, &$result, &$n, $keys) { $result[$index] = $f->inspect(); if (--$n === 0) { $array = array(); foreach ($keys as $key) { $array[$key] = $result[$key]; } $future->resolve($array); } }; }; foreach ($array as $index => $element) { $f = toFuture($element); $f->whenComplete($oncomplete($index, $f)); } return $future; }); }