PHP code example of parallite / parallite-php
1. Go to this page and download the library: Download parallite/parallite-php library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
parallite / parallite-php example snippets
sic usage - no imports needed!
$result = await(async(fn() => 'Hello World'));
echo $result; // Hello World
// Parallel execution
$p1 = async(fn() => sleep(1) && 'Task 1');
$p2 = async(fn() => sleep(1) && 'Task 2');
$p3 = async(fn() => sleep(1) && 'Task 3');
$results = await([$p1, $p2, $p3]);
// Total time: ~1s (parallel) instead of 3s (sequential)
// Promise chaining
$result = await(
async(fn() => 1 + 2)
->then(fn($n) => $n * 2)
->then(fn($n) => $n + 5)
);
echo $result; // 11
// Error handling
$result = await(
async(function () {
throw new Exception('Oops!');
})->catch(fn($e) => 'Caught: ' . $e->getMessage())
);
echo $result; // Caught: Oops!
// Fetch multiple APIs in parallel
$promises = [
'users' => async(fn() => file_get_contents('https://api.example.com/users')),
'posts' => async(fn() => file_get_contents('https://api.example.com/posts')),
'comments' => async(fn() => file_get_contents('https://api.example.com/comments')),
];
$data = await($promises);
// 3x faster than sequential fetching!
bash
composer