ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
functions.php
Go to the documentation of this file.
1 <?php
2 
4 
6 
32 function all(array $promises) {
33 
34  return new Promise(function($success, $fail) use ($promises) {
35 
36  $successCount = 0;
37  $completeResult = [];
38 
39  foreach ($promises as $promiseIndex => $subPromise) {
40 
41  $subPromise->then(
42  function($result) use ($promiseIndex, &$completeResult, &$successCount, $success, $promises) {
43  $completeResult[$promiseIndex] = $result;
44  $successCount++;
45  if ($successCount === count($promises)) {
46  $success($completeResult);
47  }
48  return $result;
49  }
50  )->error(
51  function($reason) use ($fail) {
52  $fail($reason);
53  }
54  );
55 
56  }
57  });
58 
59 }
60 
71 function race(array $promises) {
72 
73  return new Promise(function($success, $fail) use ($promises) {
74 
75  $alreadyDone = false;
76  foreach ($promises as $promise) {
77 
78  $promise->then(
79  function($result) use ($success, &$alreadyDone) {
80  if ($alreadyDone) {
81  return;
82  }
83  $alreadyDone = true;
85  },
86  function($reason) use ($fail, &$alreadyDone) {
87  if ($alreadyDone) {
88  return;
89  }
90  $alreadyDone = true;
91  $fail($reason);
92  }
93  );
94 
95  }
96 
97  });
98 
99 }
100 
101 
111 function resolve($value) {
112 
113  if ($value instanceof Promise) {
114  return $value->then();
115  } else {
116  $promise = new Promise();
117  $promise->fulfill($value);
118  return $promise;
119  }
120 
121 }
122 
129 function reject($reason) {
130 
131  $promise = new Promise();
132  $promise->reject($reason);
133  return $promise;
134 
135 }
$result
An implementation of the Promise pattern.
Definition: Promise.php:23
$promise
This example shows demonstrates the Promise api.
Definition: promise.php:16
resolve($value)
Returns a Promise that resolves with the given value.
Definition: functions.php:111
static all(array $promises)
Deprecated.
Definition: Promise.php:314
$success
Definition: Utf8Test.php:86
reject($reason=null)
Marks this promise as rejected, and set it&#39;s rejection reason.
Definition: Promise.php:161
race(array $promises)
The race function returns a promise that resolves or rejects as soon as one of the promises in the ar...
Definition: functions.php:71
error(callable $onRejected)
Alias for &#39;otherwise&#39;.
Definition: Promise.php:299