Skip to content

Commit 4013918

Browse files
authored
Merge pull request #15 from async-interop/rename
Rename to Promise
2 parents b469819 + 4e0316c commit 4013918

File tree

4 files changed

+27
-29
lines changed

4 files changed

+27
-29
lines changed

META.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# Awaitable
1+
# Promise
22

33
This document describes a few design considerations, for the main specification, refer to [the README document](README.md).
44

5-
## Recommended usage of Awaitable
5+
## Recommended usage of Promises
66

77
We are explicitly _not_ providing a chainable method, thus we are also not recommending to chain them, but rather to use coroutines in form of generators inside application code.
88

@@ -12,16 +12,14 @@ Coroutines solve the problem of the so-called callback hell very nicely and also
1212

1313
The specification proposes `when()` in order to only expose the most primitive common denominatior needed for interoperability, which is a simple callable to be executed after the resolution.
1414

15-
If implementations wish to adhere to e.g. [Promises/A+ from Javascript](https://promisesaplus.com) (which had been implemented in many PHP Promise libraries at the time of writing this specification) or implement any other methods, they still may do so; `when()` however is the fundamental interoperable primitive every `Awaitable` is guaranteed to have.
15+
If implementations wish to adhere to e.g. [Promises/A+ from Javascript](https://promisesaplus.com) (which had been implemented in many PHP Promise libraries at the time of writing this specification) or implement any other methods, they still may do so; `when()` however is the fundamental interoperable primitive every `Promise` is guaranteed to have.
1616

17-
Additionally, coroutines do not use the returned `Promise` of a `then` callback, but returning a `Promise` is required by Promises/A+. Thus there's a lot of useless object creations when using Awaitables the recommended way, which isn't cheap and adds up. This conflicts with the goal of being as lightweight as possible.
17+
Additionally, coroutines do not use the returned `Promise` of a `then` callback, but returning a `Promise` is required by Promises/A+. Thus there's a lot of useless object creations when using Promises the recommended way, which isn't cheap and adds up. This conflicts with the goal of being as lightweight as possible.
1818

1919
## Naming
2020

21-
As we are not using a thenable, which is sometimes associated with the word `Promise`, we decided against using `Promise`.
21+
Even though we are not using a thenable, `Promise` is a more recognizable and accepted name for future value placeholders.
2222

23-
Thus, `Awaitable` was a logical choice, with [HHVM already having it](https://docs.hhvm.com/hack/reference/interface/HH.Awaitable/) and PHP possibly also being some day extended to natively support an `await` keyword.
23+
## Creation of Promises
2424

25-
## Creation of Awaitables
26-
27-
Awaitable creation and managing is out of scope of this specification, as managing never shall cross library boundaries and thus does not need to be interoperable at all; each library shall resolve the Awaitable it created itself.
25+
Promise creation and managing is out of scope of this specification, as managing never shall cross library boundaries and thus does not need to be interoperable at all; each library shall resolve the Promise it created itself.

README.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
# Awaitable
1+
# Promise
22

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

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

9-
An `Awaitable` represents the eventual result of an asynchronous operation. Interaction with an `Awaitable` happens through its `when` method, which registers a callback to receive either an `Awaitable`'s eventual value or the reason why the `Awaitable` has failed.
9+
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.
1010

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

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

15-
This specification does not deal with how to create, succeed or fail `Awaitable`s, as only the consumption of `Awaitable`s is required to be interoperable.
15+
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.
1616

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

1919
## Terminology
2020

21-
1. _Awaitable_ is an object implementing `Interop\Async\Awaitable` and conforming to this specification.
22-
2. _Value_ is any legal PHP value (including `null`), except an instance of `Interop\Async\Awaitable`.
21+
1. _Promise_ is an object implementing `Interop\Async\Promise` and conforming to this specification.
22+
2. _Value_ is any legal PHP value (including `null`), except an instance of `Interop\Async\Promise`.
2323
3. _Error_ is any value that can be thrown using the `throw` statement.
24-
4. _Reason_ is an error indicating why an `Awaitable` has failed.
24+
4. _Reason_ is an error indicating why a `Promise` has failed.
2525

2626
## States
2727

28-
An `Awaitable` MUST be in one of three states: `pending`, `succeeded`, `failed`.
28+
A `Promise` MUST be in one of three states: `pending`, `succeeded`, `failed`.
2929

3030
| A promise in … state |   |
3131
|----------------------|--------|
@@ -37,7 +37,7 @@ An `Awaitable` MUST be in one of three states: `pending`, `succeeded`, `failed`.
3737

3838
## Consumption
3939

40-
An `Awaitable` MUST implement `Interop\Async\Awaitable` and thus provide a `when` method to access its current or eventual value or reason.
40+
A `Promise` MUST implement `Interop\Async\Promise` and thus provide a `when` method to access its current or eventual value or reason.
4141

4242
```php
4343
<?php
@@ -47,10 +47,10 @@ namespace Interop\Async;
4747
/**
4848
* Representation of a the future value of an asynchronous operation.
4949
*/
50-
interface Awaitable
50+
interface Promise
5151
{
5252
/**
53-
* Registers a callback to be invoked when the awaitable is resolved.
53+
* Registers a callback to be invoked when the promise is resolved.
5454
*
5555
* @param callable(\Throwable|\Exception|null $exception, mixed $result) $onResolved
5656
*
@@ -68,13 +68,13 @@ The `when` method MUST accept at least one argument:
6868
function($error, $value) { /* ... */ }
6969
```
7070

71-
Any implementation MUST at least provide these two parameters. The implementation MAY extend the `Awaitable` 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.
71+
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.
7272

7373
> **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`.
7474
75-
All registered callbacks MUST be executed in the order they were registered. If one of the callbacks throws an `Exception` or `Throwable`, it MUST be rethrown in a callable passed to `Loop::defer` so `Loop::onError` can be properly invoked by the loop. `Loop` refers to the [global event loop accessor](https://github.com/async-interop/event-loop/blob/master/src/Loop.php). The `Awaitable` implementation MUST then continue to call the remaining callbacks with the original parameters.
75+
All registered callbacks MUST be executed in the order they were registered. If one of the callbacks throws an `Exception` or `Throwable`, it MUST be rethrown in a callable passed to `Loop::defer` so `Loop::onError` can be properly invoked by the loop. `Loop` refers to the [global event loop accessor](https://github.com/async-interop/event-loop/blob/master/src/Loop.php). The `Promise` implementation MUST then continue to call the remaining callbacks with the original parameters.
7676

77-
If an `Awaitable` is resolved with another `Awaitable`, the `Awaitable` MUST keep in pending state until the passed `Awaitable` is resolved. Thus, the value of an `Awaitable` can never be an `Awaitable`.
77+
If a `Promise` is resolved with another `Promise`, the `Promise` MUST keep in pending state until the passed `Promise` is resolved. Thus, the value of a `Promise` can never be a `Promise`.
7878

7979
## Contributors
8080

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"name": "async-interop/awaitable",
3-
"description": "An awaitable interface for implementing async operations",
4-
"keywords": ["awaitable", "async", "interop"],
2+
"name": "async-interop/promise",
3+
"description": "Promise interface for implementing async operations",
4+
"keywords": ["promise", "future", "awaitable", "async", "interop"],
55
"license": "MIT",
66
"require": {
77
"php": ">=5.4"

src/Awaitable.php renamed to src/Promise.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
namespace Interop\Async;
44

55
/**
6-
* Awaitable object representing the future value of an asynchronous operation.
6+
* Promise object representing the future value of an asynchronous operation.
77
*/
8-
interface Awaitable
8+
interface Promise
99
{
1010
/**
11-
* Registers a callback to be invoked when the awaitable is resolved.
11+
* Registers a callback to be invoked when the promise is resolved.
1212
*
1313
* @param callable(\Throwable|\Exception|null $exception, mixed $result) $onResolved
1414
*

0 commit comments

Comments
 (0)