Is there any way to make a tests for asynchronous functions in php?
one text
I'm trying to understand asynchronous function in PHP. I'm using spatie async
package for this. I want to know, how can I check whether my code is working asynchrony or not?
I'm tried this code but every time I got same order in output.
use Spatie\Async\Pool;
$pool = Pool::create();
$nums = [1, 2, 3, 4, 5];
foreach ($nums as $num) {
$pool->add( function () use($num) {
$numSqr = $num * $num;
return $numSqr;
})->then( function ($numSqr) {
echo $numSqr . '<br>';
});
}
$pool->wait();
Output:
1
4
9
16
25
Source