Async "Hello World". Just for fun %)
$fn = async(static function () {
echo yield 1;
return async(static function () {
echo yield 2;
echo yield 3;
return 'Word';
});
});
$prom = async(static function () use ($fn) {
echo yield 4;
return $fn;
})->then(static function ($value) {
$promise = async(static function () use ($value) {
echo yield 5;
echo yield 6;
return $value;
});
$promise->then(static function ($value) {
echo "\nHell Or $value\n";
});
});
- Function
async
returns a Promise. - Result of a promise is an
return
value of given closure. - Every
yield
expression is an EventLoop "tick". - Every
yield Promise
orreturn Promise
waits for completion, and then returns the result.
$promise = async(fn () => 23)
$promise->then(fn ($value) => echo $value)
// 23
$promise = async(function () {
echo yield 1;
echo yield 2;
return 3;
});
$promise->then(fn ($value) => echo $value)
// 123
async(function () {
echo yield 1;
echo yield 3;
});
async(function () {
echo yield 2;
echo yield 4;
});
// 1234
async(function() {
$promise = async(fn () => 23);
echo yield $promise; // 23
});
$promise = async(function() {
return async(fn () => 23);
});
$promise->then(fn ($value) => echo $value);
// 23
TL;DR; Do not us it =)
See LICENSE