ucscode/promise
最新稳定版本:1.0.0
Composer 安装命令:
composer require ucscode/promise
包简介
A lightweight and concise implementation of Promises in PHP
README 文档
README
Promise is a simple and lightweight implementation of Javascript Promises in PHP. Promises provide a clean and structured way to work with asynchronous operations, allowing you to handle deferred execution and manage asynchronous workflows more effectively.
Features
- Promises/A+ Compliant: Follows the Promises/A+ specification for consistent behavior.
- Chaining: Chain promises together using the
thenmethod. - Error Handling: Handle errors using the
catchmethod. - Finalization: Execute code regardless of the promise's state with the
finallymethod.
Getting Started
Installation
composer require ucscode/promise
Usage
use Ucscode\Promise\Promise; $promise = new Promise(function ($resolve, $reject) { // Asynchronous operation sleep(20); $resolve("Operation Successful"); }); $promise->then( fn ($value) => "Fulfilled: $value", fn ($reason) => "Rejected: $reason" ) ->finally(fn () => "Operation complete");
Multiple Promises
The Promise::all method takes an array of promises, and collects their fulfilled values. If all promises are fulfilled, it resolves with an array of fulfilled values. If any promise is rejected, it rejects with the reason of the first rejected promise.
$promises = [ new Promise(function ($resolve) { $resolve(1); }), new Promise(function ($resolve) { $resolve(2); }), new Promise(function ($resolve) { $resolve(3); }), ]; Promise::all($promises)->then( function ($values) { // All promises fulfilled var_dump($values); // Output: array(1, 2, 3) }, fn ($reason) => "At least one promise rejected" );
Contributing
Contributions are welcome! Feel free to open issues or pull requests.
License
This project is licensed under the MIT License
统计信息
- 总下载量: 10
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-01-22