|
1 |
| -# NOTE: This package is no longer maintained. Use [react/promise](https://github.com/reactphp/promise) instead! |
2 |
| - |
3 | 1 | # Async
|
4 | 2 |
|
5 |
| -Async utilities for [ReactPHP](https://reactphp.org/). |
6 |
| - |
7 |
| -It is heavily influenced by [async.js](https://github.com/caolan/async). |
8 |
| - |
9 | 3 | [](https://github.com/reactphp/async/actions)
|
10 | 4 |
|
11 |
| -This library allows you to manage async control flow. It provides a number of |
12 |
| -combinators for continuation-passing style (aka callbacks). Instead of nesting |
13 |
| -those callbacks, you can declare them as a list, which is resolved |
14 |
| -sequentially in an async manner. |
| 5 | +Async utilities for [ReactPHP](https://reactphp.org/). |
15 | 6 |
|
| 7 | +This library allows you to manage async control flow. It provides a number of |
| 8 | +combinators for [Promise](https://github.com/reactphp/promise)-based APIs. |
| 9 | +Instead of nesting or chaining promise callbacks, you can declare them as a |
| 10 | +list, which is resolved sequentially in an async manner. |
16 | 11 | React/Async will not automagically change blocking code to be async. You need
|
17 | 12 | to have an actual event loop and non-blocking libraries interacting with that
|
18 |
| -event loop for it to work. You can use `react/event-loop` for this, but you |
19 |
| -don't have to. As long as you have a callback-based API that runs in an event |
20 |
| -loop, it can be used with this library. |
21 |
| - |
22 |
| -*You must be running inside an event loop for react/async to make any sense |
23 |
| -whatsoever!* |
| 13 | +event loop for it to work. As long as you have a Promise-based API that runs in |
| 14 | +an event loop, it can be used with this library. |
24 | 15 |
|
25 | 16 | **Table of Contents**
|
26 | 17 |
|
@@ -62,112 +53,116 @@ Async\parallel(…);
|
62 | 53 |
|
63 | 54 | ### parallel()
|
64 | 55 |
|
65 |
| -The `parallel(array<callable> $tasks, ?callable $callback = null, ?callable $errback = null): void` function can be used |
| 56 | +The `parallel(array<callable():PromiseInterface<mixed,Exception>> $tasks): PromiseInterface<array<mixed>,Exception>` function can be used |
66 | 57 | like this:
|
67 | 58 |
|
68 | 59 | ```php
|
69 | 60 | <?php
|
70 | 61 |
|
71 | 62 | use React\EventLoop\Loop;
|
| 63 | +use React\Promise\Promise; |
72 | 64 |
|
73 |
| -React\Async\parallel( |
74 |
| - array( |
75 |
| - function ($callback, $errback) { |
76 |
| - Loop::addTimer(1, function () use ($callback) { |
77 |
| - $callback('Slept for a whole second'); |
| 65 | +React\Async\parallel([ |
| 66 | + function () { |
| 67 | + return new Promise(function ($resolve) { |
| 68 | + Loop::addTimer(1, function () use ($resolve) { |
| 69 | + $resolve('Slept for a whole second'); |
78 | 70 | });
|
79 |
| - }, |
80 |
| - function ($callback, $errback) { |
81 |
| - Loop::addTimer(1, function () use ($callback) { |
82 |
| - $callback('Slept for another whole second'); |
| 71 | + }); |
| 72 | + }, |
| 73 | + function () { |
| 74 | + return new Promise(function ($resolve) { |
| 75 | + Loop::addTimer(1, function () use ($resolve) { |
| 76 | + $resolve('Slept for another whole second'); |
83 | 77 | });
|
84 |
| - }, |
85 |
| - function ($callback, $errback) { |
86 |
| - Loop::addTimer(1, function () use ($callback) { |
87 |
| - $callback('Slept for yet another whole second'); |
| 78 | + }); |
| 79 | + }, |
| 80 | + function () { |
| 81 | + return new Promise(function ($resolve) { |
| 82 | + Loop::addTimer(1, function () use ($resolve) { |
| 83 | + $resolve('Slept for yet another whole second'); |
88 | 84 | });
|
89 |
| - }, |
90 |
| - ), |
91 |
| - function (array $results) { |
92 |
| - foreach ($results as $result) { |
93 |
| - var_dump($result); |
94 |
| - } |
| 85 | + }); |
95 | 86 | },
|
96 |
| - function (Exception $e) { |
97 |
| - throw $e; |
| 87 | +])->then(function (array $results) { |
| 88 | + foreach ($results as $result) { |
| 89 | + var_dump($result); |
98 | 90 | }
|
99 |
| -); |
| 91 | +}, function (Exception $e) { |
| 92 | + echo 'Error: ' . $e->getMessage() . PHP_EOL; |
| 93 | +}); |
100 | 94 | ```
|
101 | 95 |
|
102 | 96 | ### series()
|
103 | 97 |
|
104 |
| -The `series(array<callable> $tasks, ?callable $callback = null, ?callable $errback = null): void` function can be used |
| 98 | +The `series(array<callable():PromiseInterface<mixed,Exception>> $tasks): PromiseInterface<array<mixed>,Exception>` function can be used |
105 | 99 | like this:
|
106 | 100 |
|
107 | 101 | ```php
|
108 | 102 | <?php
|
109 | 103 |
|
110 | 104 | use React\EventLoop\Loop;
|
| 105 | +use React\Promise\Promise; |
111 | 106 |
|
112 |
| -React\Async\series( |
113 |
| - array( |
114 |
| - function ($callback, $errback) { |
115 |
| - Loop::addTimer(1, function () use ($callback) { |
116 |
| - $callback('Slept for a whole second'); |
| 107 | +React\Async\series([ |
| 108 | + function () { |
| 109 | + return new Promise(function ($resolve) { |
| 110 | + Loop::addTimer(1, function () use ($resolve) { |
| 111 | + $resolve('Slept for a whole second'); |
117 | 112 | });
|
118 |
| - }, |
119 |
| - function ($callback, $errback) { |
120 |
| - Loop::addTimer(1, function () use ($callback) { |
121 |
| - $callback('Slept for another whole second'); |
| 113 | + }); |
| 114 | + }, |
| 115 | + function () { |
| 116 | + return new Promise(function ($resolve) { |
| 117 | + Loop::addTimer(1, function () use ($resolve) { |
| 118 | + $resolve('Slept for another whole second'); |
122 | 119 | });
|
123 |
| - }, |
124 |
| - function ($callback, $errback) { |
125 |
| - Loop::addTimer(1, function () use ($callback) { |
126 |
| - $callback('Slept for yet another whole second'); |
| 120 | + }); |
| 121 | + }, |
| 122 | + function () { |
| 123 | + return new Promise(function ($resolve) { |
| 124 | + Loop::addTimer(1, function () use ($resolve) { |
| 125 | + $resolve('Slept for yet another whole second'); |
127 | 126 | });
|
128 |
| - }, |
129 |
| - ), |
130 |
| - function (array $results) { |
131 |
| - foreach ($results as $result) { |
132 |
| - var_dump($result); |
133 |
| - } |
| 127 | + }); |
134 | 128 | },
|
135 |
| - function (Exception $e) { |
136 |
| - throw $e; |
| 129 | +])->then(function (array $results) { |
| 130 | + foreach ($results as $result) { |
| 131 | + var_dump($result); |
137 | 132 | }
|
138 |
| -); |
| 133 | +}, function (Exception $e) { |
| 134 | + echo 'Error: ' . $e->getMessage() . PHP_EOL; |
| 135 | +}); |
139 | 136 | ```
|
140 | 137 |
|
141 | 138 | ### waterfall()
|
142 | 139 |
|
143 |
| -The `waterfall(array<callable> $tasks, ?callable $callback = null, ?callable $errback = null): void` function can be used |
| 140 | +The `waterfall(array<callable(mixed=):PromiseInterface<mixed,Exception>> $tasks): PromiseInterface<mixed,Exception>` function can be used |
144 | 141 | like this:
|
145 | 142 |
|
146 | 143 | ```php
|
147 | 144 | <?php
|
148 | 145 |
|
149 | 146 | use React\EventLoop\Loop;
|
| 147 | +use React\Promise\Promise; |
150 | 148 |
|
151 |
| -$addOne = function ($prev, $callback = null) { |
152 |
| - if (!$callback) { |
153 |
| - $callback = $prev; |
154 |
| - $prev = 0; |
155 |
| - } |
156 |
| - |
157 |
| - Loop::addTimer(1, function () use ($prev, $callback) { |
158 |
| - $callback($prev + 1); |
| 149 | +$addOne = function ($prev = 0) { |
| 150 | + return new Promise(function ($resolve) use ($prev) { |
| 151 | + Loop::addTimer(1, function () use ($prev, $resolve) { |
| 152 | + $resolve($prev + 1); |
| 153 | + }); |
159 | 154 | });
|
160 | 155 | };
|
161 | 156 |
|
162 |
| -React\Async\waterfall(array( |
163 |
| - $addOne, |
| 157 | +React\Async\waterfall([ |
164 | 158 | $addOne,
|
165 | 159 | $addOne,
|
166 |
| - function ($prev, $callback) use ($loop) { |
167 |
| - echo "Final result is $prev\n"; |
168 |
| - $callback(); |
169 |
| - }, |
170 |
| -)); |
| 160 | + $addOne |
| 161 | +])->then(function ($prev) { |
| 162 | + echo "Final result is $prev\n"; |
| 163 | +}, function (Exception $e) { |
| 164 | + echo 'Error: ' . $e->getMessage() . PHP_EOL; |
| 165 | +}); |
171 | 166 | ```
|
172 | 167 |
|
173 | 168 | ## Todo
|
@@ -210,3 +205,5 @@ $ php vendor/bin/phpunit
|
210 | 205 | ## License
|
211 | 206 |
|
212 | 207 | MIT, see [LICENSE file](LICENSE).
|
| 208 | + |
| 209 | +This project is heavily influenced by [async.js](https://github.com/caolan/async). |
0 commit comments