定制 ucscode/promise 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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 then method.
  • Error Handling: Handle errors using the catch method.
  • Finalization: Execute code regardless of the promise's state with the finally method.

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

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-01-22