Skip to content

Instantly share code, notes, and snippets.

@f2r
Last active May 31, 2023 14:58
Show Gist options
  • Save f2r/233047300462cca9d8b14a07a888fd51 to your computer and use it in GitHub Desktop.
Save f2r/233047300462cca9d8b14a07a888fd51 to your computer and use it in GitHub Desktop.
chatGPT 3.5 prompt: create a PHP RFC for promises

Title: Asynchronous PHP: Promises RFC

  • Version: 1.0
  • Date: 2023-05-31
  • Author: [Your Name]
  • Status: Draft
  • First Published at: [URL]

Introduction

This RFC proposes the addition of native promises to PHP, enabling developers to write asynchronous code in a more concise and readable manner. Promises are a well-established concept in other programming languages and can greatly simplify asynchronous programming, making it easier to manage and compose asynchronous operations.

Motivation

Asynchronous programming in PHP traditionally involves using callbacks or generators, which can lead to callback hell or convoluted generator-based code. Introducing native promises will provide a standardized, more readable, and maintainable way to work with asynchronous operations.

Proposal

Promise Class

Introduce a new Promise class that represents the result of an asynchronous operation. The Promise class will encapsulate the state of the operation and provide methods to handle success, failure, and chaining of operations.

Promise States

The Promise class will have three states:

  • Pending: The initial state when the asynchronous operation is in progress.
  • Fulfilled: The state when the asynchronous operation completes successfully.
  • Rejected: The state when the asynchronous operation encounters an error.

Core API

Promise::resolve($value): Returns a new promise resolved with the given value.

Promise::reject($reason): Returns a new promise rejected with the given reason.

Promise::all(array $promises): Promise: Returns a new promise that is fulfilled when all promises in the given array are fulfilled. If any of the promises are rejected, the returned promise will be rejected with the reason of the first rejected promise.

Promise::race(array $promises): Promise: Returns a new promise that is settled with the result of the first promise in the given array that settles (fulfilled or rejected).

Promise Methods

then(callable $onFulfilled, callable $onRejected = null): Promise: Attaches callbacks for the success and failure cases of the promise. The onFulfilled callback will be called when the promise is fulfilled, and the onRejected callback will be called when the promise is rejected.

catch(callable $onRejected): Promise: Attaches a callback for the failure case of the promise. This is a shorthand for then(null, $onRejected).

finally(callable $onFinally): Promise: Attaches a callback that will be called when the promise is settled, regardless of whether it is fulfilled or rejected.

Example Usage

<?php
$promise = new Promise(function ($resolve, $reject) {
    // Asynchronous operation
    // Call $resolve($value) when successful
    // Call $reject($reason) when an error occurs
});

$promise
    ->then(function ($value) {
        // Handle the fulfilled value
    })
    ->catch(function ($reason) {
        // Handle the rejection reason
    });

Compatibility

This RFC proposes a backward-compatible addition to PHP, ensuring that existing code will not be affected by the introduction of promises.

Open Issues

Error handling and propagation: The proposal does not cover error handling and propagation in detail. Further discussion is required to determine the best approach for handling errors within promises. Integration with existing asynchronous APIs: This RFC does not address the integration of promises with existing asynchronous APIs in PHP, such as cURL or database libraries. Compatibility and integration guidelines should be established.

Conclusion

The introduction of native promises in PHP will greatly enhance the language's capabilities for asynchronous programming. Promises provide a cleaner and more maintainable syntax for handling asynchronous operations, making PHP a more modern and developer-friendly language in the asynchronous world. Feedback and suggestions from the PHP community are welcomed to improve and refine this proposal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment