Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ function resolve($promiseOrValue = null)
$canceller = [$promiseOrValue, 'cancel'];
}

if (method_exists($promiseOrValue, 'done')) {
return new Promise(function ($resolve, $reject, $notify) use ($promiseOrValue) {
$promiseOrValue->done($resolve, $reject, $notify);
}, $canceller);
}

return new Promise(function ($resolve, $reject, $notify) use ($promiseOrValue) {
$promiseOrValue->then($resolve, $reject, $notify);
}, $canceller);
Expand Down
14 changes: 14 additions & 0 deletions tests/FunctionResolveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ public function shouldResolveAThenable()
);
}

/** @test */
public function shouldResolveADonable()
{
$this->setExpectedException('\Exception', 'UnhandledRejectionException');

$exception = new \Exception('UnhandledRejectionException');

$donable = new SimpleRejectedTestDonable($exception);

resolve($donable)
->then()
->done();
}

/** @test */
public function shouldResolveACancellableThenable()
{
Expand Down
23 changes: 23 additions & 0 deletions tests/fixtures/SimpleRejectedTestDonable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace React\Promise;

class SimpleRejectedTestDonable
{
private $exception;

public function __construct(\Exception $exception)
{
$this->exception = $exception;
}

public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
{
return new self($this->exception);
}

public function done(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
{
throw $this->exception;
}
}