Handling asynchronous behaviour in PHP
step1('foo', function($result1) {
// step2 depends on result of step1
step2($resultOne, function($result2) {
// Do something with result of step2
});
});$counter = 0;
$handler = function($result) use (&$counter) {
$counter += 1;
if ($counter === 2) {
// Do something...
}
};
doSomething('foo', $handler);
doSomethingElse('bar', $handler);promiseMe('foo')
->then(function($result) {
// do something with result
})
->otherwise(function($error) {
// handle error
})
->always(function() {
// clean up
});promiseMe('foo')
->then(function($result) {
return promiseMe('bar');
})
->then(function($result2) {
// do something with second result
});use React\Promise;
Promise\all([
promiseMe('foo'),
promiseMe('bar')
])
->then(function($results) {
// Do something with array of results
})->catch(function() {
// Handle error immediately
});use React\Promise\Deferred;
function promiseMe($params) {
$deferred = new Deferred();
doSomething($params, function($result) use ($deferred) {
$deferred->resolve($result);
});
return $deferred->promise();
}