Skip to content

Add concurrent() function#468

Open
rela589n wants to merge 1 commit into
amphp:3.xfrom
rela589n:feature-concurrent-function
Open

Add concurrent() function#468
rela589n wants to merge 1 commit into
amphp:3.xfrom
rela589n:feature-concurrent-function

Conversation

@rela589n

Copy link
Copy Markdown

This PR adds a concurrent() function for creating a fixed set of concurrent operations:

function concurrent(array $closures): array
{
    return \array_map(async(...), $closures);
}

When combined with other functions through pipe operator, it produces easy-to-comprehend concurrent code:

$results = [
    fn () => $httpClient->request(new Request('https://google.com', 'HEAD')),
    fn () => $httpClient->request(new Request('https://bing.com', 'HEAD')),
] |> concurrent(...) |> await(...);
$firstReachableApi = [
    fn () => $httpClient->request(new Request('https://a.api.com', 'HEAD')),
    fn () => $httpClient->request(new Request('https://b.api.com', 'HEAD')),
] |> concurrent(...) |> awaitAny(...);

@rela589n rela589n force-pushed the feature-concurrent-function branch from bc6805d to 59aae0f Compare June 29, 2026 10:36
@xpader

xpader commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

No meaning fot this..

@rela589n

Copy link
Copy Markdown
Author

@trowski , @kelunik , once this is merged, I think I'll open a dedicated PR for settle() so that #467 can be closed. concurrent() is a good thing for settle() README examples, so I'll linger opening a new PR till this is merged.

Comment thread src/functions.php
*
* @return array<Tk, Future<Tv>> A Future for each closure, with keys preserved.
*/
function concurrent(array $closures): array

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to support iterable here?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a problem with me to make it iterable / array,
yet the decision is yours, guys.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants