Skip to content

Commit

Permalink
Merge pull request #35 from async-interop/krakjoe-rewording
Browse files Browse the repository at this point in the history
Rewording of README and docblock
  • Loading branch information
bwoebi authored Jan 6, 2017
2 parents 5cc82dd + 72a08e5 commit 511ae43
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 31 deletions.
34 changes: 10 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# Promise

The purpose of this specification is to provide a common interface for simple placeholder objects returned from async operations. This allows libraries and components from different vendors to create coroutines regardless of the used placeholder implementation. This specification is not designed to replace promise implementations that may be chained. Instead, this interface may be extended by promise implementations.
The purpose of this specification is to provide a common interface for simple placeholder objects returned from async operations. This allows libraries and components from different vendors to create coroutines regardless of the placeholder implementation used. This specification is not designed to replace promise implementations that may be chained. Instead, the common interface may be extended by promise implementations.

The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD",
"SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be
interpreted as described in [RFC 2119][].

A `Promise` represents the eventual result of an asynchronous operation. Interaction with a `Promise` happens through its `when()` method, which registers a callback to receive either a `Promise`'s eventual value or the reason why the `Promise` has failed.
A `Promise` represents the eventual result of an asynchronous operation. Interaction with a `Promise` happens through its `when()` method, which registers a callback to receive either a `Promise`'s eventual value, or reason for failure.

`Promise` is the fundamental primitive in asynchronous programming. It should be as lightweight as possible, as any cost adds up significantly.

This specification defines the absolute minimums for interoperable coroutines, which can be implemented in PHP using generators.
This specification defines the absolute minimum for interoperable coroutines, which can be implemented in PHP using generators.

This specification does not deal with how to create, succeed or fail `Promise`s, as only the consumption of `Promise`s is required to be interoperable.
This specification does not deal with how a `Promise` should be created, succeed, or fail, as only the consumption of `Promise` is required to be interoperable.

For further design explanations and notes, please refer to [the meta document](META.md).

Expand All @@ -39,7 +39,7 @@ A `Promise` is resolved once it either succeeded or failed.

## Consumption

A `Promise` MUST implement `AsyncInterop\Promise` and thus provide a `when()` method to access its current or eventual value or reason.
A `Promise` MUST implement `AsyncInterop\Promise` and thus provide a `when()` method to access its value or reason.

```php
<?php
Expand All @@ -54,34 +54,20 @@ interface Promise
/**
* Registers a callback to be invoked when the promise is resolved.
*
* The callback receives `null` as first parameter and `$value` as second parameter on success. It receives the
* failure reason as first parameter and `null` as second parameter on failure.
*
* If the promise is already resolved, the callback MUST be executed immediately.
*
* Warning: If you use type declarations for `$value`, be sure to make them accept `null` in case of failures.
*
* @param callable(\Throwable|\Exception|null $exception, mixed $value) $onResolved Callback to be executed.
* @param callable(\Throwable|\Exception|null $exception, $value) @onResolved `$reason` shall be `null` on
* success, `$value` shall be `null` on failure.
*
* @return void
* @return mixed Return type and value are unspecified.
*/
public function when(callable $onResolved);
}
```

The `when()` method MUST accept at least one argument:

`$callback` – A callable conforming to the following signature:

```php
function($error, $value) { /* ... */ }
```

Any implementation MUST at least provide these two parameters. The implementation MAY extend the `Promise` interface with additional parameters passed to the callback. Further arguments to `when()` MUST have default values, so `when()` can always be called with only one argument. `when()` MAY NOT return a value. `when()` MUST NOT throw exceptions bubbling up from a callback invocation.

> **NOTE:** The signature doesn't specify a type for `$error`. This is due to the new `Throwable` interface introduced in PHP 7. As this specification is PHP 5 compatible, we can use neither `Throwable` nor `Exception`.
All callbacks registered before the `Promise` is resolved MUST be executed in the order they were registered after the `Promise` has been resolved. Callbacks registered after the resolution MUST be executed immediately.

All callbacks registered before the resolution MUST be executed in the order they were registered. Callbacks registered after the resolution MUST be executed immediately. If one of the callbacks throws an `Exception` or `Throwable`, it MUST be forwarded to `AsyncInterop\Promise\ErrorHandler::notify`. The `Promise` implementation MUST then continue to call the remaining callbacks with the original parameters.
The invocation of `Promise::when()` MUST NOT throw exceptions bubbling up from a `$onResolved` invocation. If one of the callbacks throws an `Exception` or `Throwable`, it MUST be forwarded to `AsyncInterop\Promise\ErrorHandler::notify`. The `Promise` implementation MUST then continue to call the remaining callbacks with the original parameters.

Registered callbacks MUST NOT be called from a file with strict types enabled (`declare(strict_types=1)`).

Expand Down
10 changes: 3 additions & 7 deletions src/Promise.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,12 @@ interface Promise
/**
* Registers a callback to be invoked when the promise is resolved.
*
* The callback receives `null` as first parameter and `$value` as second parameter on success. It receives the
* failure reason as first parameter and `null` as second parameter on failure.
*
* If the promise is already resolved, the callback MUST be executed immediately.
*
* Warning: If you use type declarations for `$value`, be sure to make them accept `null` in case of failures.
*
* @param callable(\Throwable|\Exception|null $exception, mixed $value) $onResolved Callback to be executed.
* @param callable(\Throwable|\Exception|null $exception, $value) @onResolved `$reason` shall be `null` on
* success, `$value` shall be `null` on failure.
*
* @return void
* @return mixed Return type and value are unspecified.
*/
public function when(callable $onResolved);
}

0 comments on commit 511ae43

Please sign in to comment.