Skip to content

Commit 5137c81

Browse files
committed
Remove map() function
1 parent 51638e7 commit 5137c81

File tree

3 files changed

+28
-191
lines changed

3 files changed

+28
-191
lines changed

README.md

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ Table of Contents
4242
* [all()](#all)
4343
* [race()](#race)
4444
* [any()](#any)
45-
* [map()](#map)
4645
4. [Examples](#examples)
4746
* [How to use Deferred](#how-to-use-deferred)
4847
* [How promise forwarding works](#how-promise-forwarding-works)
@@ -363,7 +362,7 @@ once all consumers called the `cancel()` method of the promise.
363362

364363
### Functions
365364

366-
Useful functions for creating, joining and mapping collections of promises.
365+
Useful functions for creating and joining collections of promises.
367366

368367
All functions working on promise collections (like `all()`, `race()`,
369368
etc.) support cancellation. This means, if you call `cancel()` on the returned
@@ -439,18 +438,6 @@ which holds all rejection reasons. The rejection reasons can be obtained with
439438
The returned promise will also reject with a `React\Promise\Exception\LengthException`
440439
if `$promisesOrValues` contains 0 items.
441440

442-
#### map()
443-
444-
```php
445-
$promise = React\Promise\map(array $promisesOrValues, callable $mapFunc);
446-
```
447-
448-
Traditional map function, similar to `array_map()`, but allows input to contain
449-
promises and/or values, and `$mapFunc` may return either a value or a promise.
450-
451-
The map function receives each item as argument, where item is a fully resolved
452-
value of a promise or value in `$promisesOrValues`.
453-
454441
Examples
455442
--------
456443

src/functions.php

Lines changed: 27 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,33 @@ function reject(\Throwable $reason): PromiseInterface
7373
*/
7474
function all(array $promisesOrValues): PromiseInterface
7575
{
76-
return map($promisesOrValues, function ($val) {
77-
return $val;
78-
});
76+
if (!$promisesOrValues) {
77+
return resolve([]);
78+
}
79+
80+
$cancellationQueue = new Internal\CancellationQueue();
81+
82+
return new Promise(function ($resolve, $reject) use ($promisesOrValues, $cancellationQueue): void {
83+
$toResolve = \count($promisesOrValues);
84+
$values = [];
85+
86+
foreach ($promisesOrValues as $i => $promiseOrValue) {
87+
$cancellationQueue->enqueue($promiseOrValue);
88+
$values[$i] = null;
89+
90+
resolve($promiseOrValue)
91+
->done(
92+
function ($mapped) use ($i, &$values, &$toResolve, $resolve): void {
93+
$values[$i] = $mapped;
94+
95+
if (0 === --$toResolve) {
96+
$resolve($values);
97+
}
98+
},
99+
$reject
100+
);
101+
}
102+
}, $cancellationQueue);
79103
}
80104

81105
/**
@@ -168,49 +192,6 @@ function any(array $promisesOrValues): PromiseInterface
168192
}, $cancellationQueue);
169193
}
170194

171-
/**
172-
* Traditional map function, similar to `array_map()`, but allows input to contain
173-
* promises and/or values, and `$mapFunc` may return either a value or a promise.
174-
*
175-
* The map function receives each item as argument, where item is a fully resolved
176-
* value of a promise or value in `$promisesOrValues`.
177-
*
178-
* @param array $promisesOrValues
179-
* @param callable $mapFunc
180-
* @return PromiseInterface
181-
*/
182-
function map(array $promisesOrValues, callable $mapFunc): PromiseInterface
183-
{
184-
if (!$promisesOrValues) {
185-
return resolve([]);
186-
}
187-
188-
$cancellationQueue = new Internal\CancellationQueue();
189-
190-
return new Promise(function ($resolve, $reject) use ($promisesOrValues, $mapFunc, $cancellationQueue): void {
191-
$toResolve = \count($promisesOrValues);
192-
$values = [];
193-
194-
foreach ($promisesOrValues as $i => $promiseOrValue) {
195-
$cancellationQueue->enqueue($promiseOrValue);
196-
$values[$i] = null;
197-
198-
resolve($promiseOrValue)
199-
->then($mapFunc)
200-
->done(
201-
function ($mapped) use ($i, &$values, &$toResolve, $resolve): void {
202-
$values[$i] = $mapped;
203-
204-
if (0 === --$toResolve) {
205-
$resolve($values);
206-
}
207-
},
208-
$reject
209-
);
210-
}
211-
}, $cancellationQueue);
212-
}
213-
214195
/**
215196
* @internal
216197
*/

tests/FunctionMapTest.php

Lines changed: 0 additions & 131 deletions
This file was deleted.

0 commit comments

Comments
 (0)