diff options
Diffstat (limited to 'vendor/sabre/event/lib/Promise/functions.php')
-rw-r--r-- | vendor/sabre/event/lib/Promise/functions.php | 53 |
1 files changed, 30 insertions, 23 deletions
diff --git a/vendor/sabre/event/lib/Promise/functions.php b/vendor/sabre/event/lib/Promise/functions.php index 986fe2b00..275492cbc 100644 --- a/vendor/sabre/event/lib/Promise/functions.php +++ b/vendor/sabre/event/lib/Promise/functions.php @@ -1,6 +1,4 @@ -<?php - -declare(strict_types=1); +<?php declare (strict_types=1); namespace Sabre\Event\Promise; @@ -16,6 +14,7 @@ use Throwable; * @license http://sabre.io/license/ Modified BSD License */ + /** * This function takes an array of Promises, and returns a Promise that * resolves when all of the given arguments have resolved. @@ -25,17 +24,17 @@ use Throwable; * * This array will be in the exact same order as the array of input promises. * - * If any of the given Promises fails, the returned promise will immediately + * If any of the given Promises fails, the returned promise will immidiately * fail with the first Promise that fails, and its reason. * * @param Promise[] $promises */ -function all(array $promises): Promise -{ - return new Promise(function ($success, $fail) use ($promises) { +function all(array $promises) : Promise { + + return new Promise(function($success, $fail) use ($promises) { + if (empty($promises)) { $success([]); - return; } @@ -43,23 +42,25 @@ function all(array $promises): Promise $completeResult = []; foreach ($promises as $promiseIndex => $subPromise) { + $subPromise->then( - function ($result) use ($promiseIndex, &$completeResult, &$successCount, $success, $promises) { + function($result) use ($promiseIndex, &$completeResult, &$successCount, $success, $promises) { $completeResult[$promiseIndex] = $result; - ++$successCount; + $successCount++; if ($successCount === count($promises)) { $success($completeResult); } - return $result; } )->otherwise( - function ($reason) use ($fail) { + function($reason) use ($fail) { $fail($reason); } ); + } }); + } /** @@ -71,20 +72,22 @@ function all(array $promises): Promise * * @param Promise[] $promises */ -function race(array $promises): Promise -{ - return new Promise(function ($success, $fail) use ($promises) { +function race(array $promises) : Promise { + + return new Promise(function($success, $fail) use ($promises) { + $alreadyDone = false; foreach ($promises as $promise) { + $promise->then( - function ($result) use ($success, &$alreadyDone) { + function($result) use ($success, &$alreadyDone) { if ($alreadyDone) { return; } $alreadyDone = true; $success($result); }, - function ($reason) use ($fail, &$alreadyDone) { + function($reason) use ($fail, &$alreadyDone) { if ($alreadyDone) { return; } @@ -92,10 +95,14 @@ function race(array $promises): Promise $fail($reason); } ); + } + }); + } + /** * Returns a Promise that resolves with the given value. * @@ -104,25 +111,25 @@ function race(array $promises): Promise * * @param mixed $value */ -function resolve($value): Promise -{ +function resolve($value) : Promise { + if ($value instanceof Promise) { return $value->then(); } else { $promise = new Promise(); $promise->fulfill($value); - return $promise; } + } /** * Returns a Promise that will reject with the given reason. */ -function reject(Throwable $reason): Promise -{ +function reject(Throwable $reason) : Promise { + $promise = new Promise(); $promise->reject($reason); - return $promise; + } |