Skip to content

Instantly share code, notes, and snippets.

@mbonneau
Last active December 29, 2016 19:37
Show Gist options
  • Save mbonneau/1461a357bed1151a46c70a9a545f5b79 to your computer and use it in GitHub Desktop.
Save mbonneau/1461a357bed1151a46c70a9a545f5b79 to your computer and use it in GitHub Desktop.
<?php
use Interop\Async\Promise;
use Rx\Observable;
function toLazyPromise(Observable $observable) {
return new class($observable) implements Promise {
/** @var Promise */
private $promise;
private $sourceObservable;
public function __construct(Observable $sourceObservable)
{
$this->sourceObservable = $sourceObservable;
}
public function when(callable $onResolved)
{
if (!$this->promise) {
$this->promise = $this->sourceObservable->toPromise();
}
return $this->promise->when($onResolved);
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment