Some common methods
Chaining Promises
function testPromise1() : Promise {
return new Promise(function ($resolve, $reject) {
System::setTimeout(function () use ($resolve) {
$resolve("A");
}, 1000);
});
}
function testPromise2() : Promise {
return new Promise(function ($resolve, $reject) {
System::setTimeout(function () use ($resolve) {
$resolve("B");
}, 1000);
});
}
function testPromise3() : Promise {
return new Promise(function ($resolve, $reject) {
System::setTimeout(function () use ($resolve) {
$resolve("C");
}, 1000);
});
}
function testPromise4() : Promise {
return new Promise(function ($resolve, $reject) {
System::setTimeout(function () use ($resolve) {
$resolve("D");
}, 1000);
});
}
testPromise1()->then(function ($value) {
var_dump($value);
return testPromise2();
})->then(function ($value) {
var_dump($value);
return testPromise3();
})->then(function ($value) {
var_dump($value);
return testPromise4();
})->then(function ($value) {
var_dump($value);
})->catch(function ($value) {
var_dump($value);
})->finally(function() {
var_dump("Complete!");
});
Promise Arrow
Promise::c(fn($resolve) => $resolve("OK"))->then(fn($result) => var_dump($result));
Advanced Mix Async Await + Promise
function request($type) {
return new Promise(function ($resolve, $reject) use ($type) {
System::setTimeout(function () use ($resolve, $reject, $type) {
$type === 'success' ? $resolve('success') : $reject('error');
}, 1000);
});
}
function getData() {
new Async(function () {
[$data, $error] = Async::await(handleRequest(request('success')));
if ($error) {
echo $error;
} else {
echo $data;
}
});
}
function handleRequest(Promise $promise) {
return $promise->then(fn($data) => [$data, null])->catch(fn($error) => [null, $error]);
}
getData();
Last updated