All Projects → async-interop → promise

async-interop / promise

Licence: MIT license
Common interface for simple asynchronous placeholders.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to promise

Qtpromise
Promises/A+ implementation for Qt/C++
Stars: ✭ 137 (+107.58%)
Mutual labels:  asynchronous, promise
Promise
Asynchronous Programming with Promises
Stars: ✭ 15 (-77.27%)
Mutual labels:  asynchronous, promise
Tascalate Concurrent
Implementation of blocking (IO-Bound) cancellable java.util.concurrent.CompletionStage and related extensions to java.util.concurrent.ExecutorService-s
Stars: ✭ 144 (+118.18%)
Mutual labels:  asynchronous, promise
Cofx
A node and javascript library that helps developers describe side-effects as data in a declarative, flexible API.
Stars: ✭ 72 (+9.09%)
Mutual labels:  asynchronous, promise
nancy
How JavaScript Promise Works
Stars: ✭ 26 (-60.61%)
Mutual labels:  asynchronous, promise
Kitchen Async
A Promise library for ClojureScript, or a poor man's core.async
Stars: ✭ 128 (+93.94%)
Mutual labels:  asynchronous, promise
Event Loop
An event loop interface for interoperability in PHP.
Stars: ✭ 170 (+157.58%)
Mutual labels:  asynchronous, interoperability
Thunks
A small and magical composer for all JavaScript asynchronous.
Stars: ✭ 523 (+692.42%)
Mutual labels:  asynchronous, promise
async
Synchronization and asynchronous computation package for Go
Stars: ✭ 104 (+57.58%)
Mutual labels:  asynchronous, promise
WebsocketPromisify
Makes websocket's API just like REST with Promise-like API, with native Promises.
Stars: ✭ 18 (-72.73%)
Mutual labels:  asynchronous, promise
Before After Hook
wrap methods with before/after hooks
Stars: ✭ 49 (-25.76%)
Mutual labels:  asynchronous, promise
PromisedFuture
A Swift based Future/Promises framework to help writing asynchronous code in an elegant way
Stars: ✭ 75 (+13.64%)
Mutual labels:  asynchronous, promise
Franticapparatus
Type and memory safe promises for Swift, supports cancellation
Stars: ✭ 27 (-59.09%)
Mutual labels:  asynchronous, promise
Rubico
[a]synchronous functional programming
Stars: ✭ 133 (+101.52%)
Mutual labels:  asynchronous, promise
Vue Loadable
⏳ Improve your loading state control with pretty simple methods and helpers.
Stars: ✭ 23 (-65.15%)
Mutual labels:  asynchronous, promise
Metasync
Asynchronous Programming Library for JavaScript & Node.js
Stars: ✭ 164 (+148.48%)
Mutual labels:  asynchronous, promise
Creed
Sophisticated and functionally-minded async with advanced features: coroutines, promises, ES2015 iterables, fantasy-land
Stars: ✭ 265 (+301.52%)
Mutual labels:  asynchronous, promise
Asyncro
⛵️ Beautiful Array utilities for ESnext async/await ~
Stars: ✭ 487 (+637.88%)
Mutual labels:  asynchronous, promise
promise4j
Fluent promise framework for Java
Stars: ✭ 20 (-69.7%)
Mutual labels:  asynchronous, promise
do
Simplest way to manage asynchronicity
Stars: ✭ 33 (-50%)
Mutual labels:  asynchronous, promise

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 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 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 minimum for interoperable coroutines, which can be implemented in PHP using generators.

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.

Terminology

  1. Promise is an object implementing AsyncInterop\Promise and conforming to this specification.
  2. Value is any legal PHP value (including null), but not an instance of AsyncInterop\Promise.
  3. Error is any value that can be thrown using the throw statement.
  4. Reason is an error indicating why a Promise has failed.

States

A Promise MUST be in one of three states: pending, succeeded, failed.

A promise in … state  
pending
  • MAY transition to either the succeeded or failed state.
succeeded
  • MUST NOT transition to any other state.
  • MUST have a value which MUST NOT change.*
failed
  • MUST NOT transition to any other state.
  • MUST have a reason which MUST NOT change.*
  • Must not change refers to the reference being immutable in case of an object, not the object itself being immutable.

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 value or reason.

<?php

namespace AsyncInterop;

/**
 * Representation of the future value of an asynchronous operation.
 */
interface Promise
{
    /**
     * Registers a callback to be invoked when the promise is resolved.
     *
     * If the promise is already resolved, the callback MUST be executed immediately.
     *
     * @param callable(\Throwable|\Exception|null $reason, $value) $onResolved `$reason` shall be `null` on
     *     success, `$value` shall be `null` on failure.
     *
     * @return mixed Return type and value are unspecified.
     */
    public function when(callable $onResolved);
}

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.

The invocation of Promise::when() MUST NOT throw exceptions bubbling up from an $onResolved invocation. If one of the callbacks throws an Exception or Throwable, the Promise implementation MUST catch it and call AsyncInterop\Promise\ErrorHandler::notify() with the Exception or Throwable as first argument. 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)).

Error Handling

Uncaught exceptions thrown from callbacks registered to Promise::when() are forwarded to the ErrorHandler by Promise implementations. ErrorHandler::set() can be used to register a callable to handle these exceptions gracefully, e.g. by logging them. In case the handler throws again or is not set, an E_USER_ERROR is triggered. If a PHP error handler is set using set_error_handler and it throws, a short message is written to STDERR and the program exits with code 255. Thus, it's RECOMMENDED to set an error handler and ensure it doesn't throw, especially if the PHP error handler is set up to convert errors to exceptions.

Contributors

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].