Skip to content

Instantly share code, notes, and snippets.

@trowski
Created October 24, 2021 06:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trowski/7ad7584087ca554cbe097a542ea04eb3 to your computer and use it in GitHub Desktop.
Save trowski/7ad7584087ca554cbe097a542ea04eb3 to your computer and use it in GitHub Desktop.
<?php
class Future
{
public function finally(callable $onSettle): self
{
$state = new FutureState();
$this->state->subscribe(static function (?\Throwable $error, mixed $value) use ($state, $onSettle): void {
try {
try {
if ($error) {
throw $error;
}
} finally {
$onSettle();
}
$state->complete($value);
} catch (\Throwable $exception) {
$state->error($exception);
}
});
return new self($state);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment