/usr/share/php/React/Promise/functions.php is in php-react-promise 2.1.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | <?php
namespace React\Promise;
function resolve($promiseOrValue = null)
{
if ($promiseOrValue instanceof PromiseInterface) {
return $promiseOrValue;
}
return new FulfilledPromise($promiseOrValue);
}
function reject($promiseOrValue = null)
{
if ($promiseOrValue instanceof PromiseInterface) {
return $promiseOrValue->then(function ($value) {
return new RejectedPromise($value);
});
}
return new RejectedPromise($promiseOrValue);
}
function all($promisesOrValues)
{
return map($promisesOrValues, function ($val) {
return $val;
});
}
function race($promisesOrValues)
{
return resolve($promisesOrValues)
->then(function ($array) {
if (!is_array($array) || !$array) {
return resolve();
}
return new Promise(function ($resolve, $reject, $progress) use ($array) {
foreach ($array as $promiseOrValue) {
resolve($promiseOrValue)
->then($resolve, $reject, $progress);
}
});
});
}
function any($promisesOrValues)
{
return some($promisesOrValues, 1)
->then(function ($val) {
return array_shift($val);
});
}
function some($promisesOrValues, $howMany)
{
return resolve($promisesOrValues)
->then(function ($array) use ($howMany) {
if (!is_array($array) || !$array || $howMany < 1) {
return resolve([]);
}
return new Promise(function ($resolve, $reject, $progress) use ($array, $howMany) {
$len = count($array);
$toResolve = min($howMany, $len);
$toReject = ($len - $toResolve) + 1;
$values = [];
$reasons = [];
foreach ($array as $i => $promiseOrValue) {
$fulfiller = function ($val) use ($i, &$values, &$toResolve, $toReject, $resolve) {
if ($toResolve < 1 || $toReject < 1) {
return;
}
$values[$i] = $val;
if (0 === --$toResolve) {
$resolve($values);
}
};
$rejecter = function ($reason) use ($i, &$reasons, &$toReject, $toResolve, $reject) {
if ($toResolve < 1 || $toReject < 1) {
return;
}
$reasons[$i] = $reason;
if (0 === --$toReject) {
$reject($reasons);
}
};
resolve($promiseOrValue)
->then($fulfiller, $rejecter, $progress);
}
});
});
}
function map($promisesOrValues, callable $mapFunc)
{
return resolve($promisesOrValues)
->then(function ($array) use ($mapFunc) {
if (!is_array($array) || !$array) {
return resolve([]);
}
return new Promise(function ($resolve, $reject, $progress) use ($array, $mapFunc) {
$toResolve = count($array);
$values = [];
foreach ($array as $i => $promiseOrValue) {
resolve($promiseOrValue)
->then($mapFunc)
->then(
function ($mapped) use ($i, &$values, &$toResolve, $resolve) {
$values[$i] = $mapped;
if (0 === --$toResolve) {
$resolve($values);
}
},
$reject,
$progress
);
}
});
});
}
function reduce($promisesOrValues, callable $reduceFunc , $initialValue = null)
{
return resolve($promisesOrValues)
->then(function ($array) use ($reduceFunc, $initialValue) {
if (!is_array($array)) {
$array = [];
}
$total = count($array);
$i = 0;
// Wrap the supplied $reduceFunc with one that handles promises and then
// delegates to the supplied.
$wrappedReduceFunc = function ($current, $val) use ($reduceFunc, $total, &$i) {
return resolve($current)
->then(function ($c) use ($reduceFunc, $total, &$i, $val) {
return resolve($val)
->then(function ($value) use ($reduceFunc, $total, &$i, $c) {
return $reduceFunc($c, $value, $i++, $total);
});
});
};
return array_reduce($array, $wrappedReduceFunc, $initialValue);
});
}
|