PHP's Promse implementation depends on the Swoole module.

Overview

php-promise-swoole

PHP's Promse implementation depends on the Swoole module.

Promise::allsettled([
        /** Timer 调用 */
        /** Timer call */
        new Promise(function($resolve) { Swoole\Timer::after(100, function() use ($resolve){ $resolve(1); }); }),

        /** 函数式调用, 协程 sleep 的等待 */
        /** Functional call, waiting for the coroutine sleep */
        Promise(function($resolve, $reject) { Co::sleep(0.01); $reject(2); }),

        /** 使用带参函数时, 参数名必须是 `$resolve` */
        /** When using a function with parameters, the parameter name must be `$resolve` */
        function($resolve) { Swoole\Timer::after(100, function() use ($resolve){ $resolve(3); }); },
    ],
    /** 无参的函数, 将以结果来判断是调用 resolve 或者 reject */
    /** For functions without parameters, the result will be used to determine whether to call resolve or reject */
    function() { return 4; },

    /** 同上, 直接根据内容判断 */
    /** Same as above, judge directly based on the content */
    '5', false,

    /** 静态调用 */
    /** Static call */
    Promise::reject(6),
    Promise::resolve(7),

    /** The method of using Timer directly, this method will only call resolve */
    Swoole\Timer::after(200, function() { var_dump('8'); }),

    /** 如果是使用了 tick 则会在第一次调用完成时, 自动清理该定时器, 这样做的原因是为了避免内存泄露啊. */
    /** If tick is used, the timer will be automatically cleaned up when the first call is completed. The reason for this is to avoid memory leaks. */
    Swoole\Timer::tick(200, function() { var_dump('9'); })
)->then(function($response){
    var_dump($response);
});

// basic example
$promise = new Promise(function(callable $resolve, callable $reject){
        Co::sleep(1); $resolve();
    });

$promise->then(function(){var_dump('then');})
    ->catch(function(){var_dump('catch');})
    ->finally(function(){var_dump('finally');});

// static example
Promise::reject()->catch(function(){var_dump('static reject catch');});
Promise::resolve()->then(function(){var_dump('static resolve catch');});

// chain example
Promise::resolve(111)->then(new Promise(function($resolve, $reject) {
    Co::sleep(0.1); $resolve(222);
}))->then(function($response) { var_dump($response); });

(new Promise(function(callable $resolve, callable $reject){
    Co::sleep(1); var_dump('promise 1 over'); $resolve(1);
}))->then(function(){
    var_dump('promise 2 enter');
    return new Promise(function(callable $resolve, callable $reject){
        Co::sleep(1); var_dump('promise 2 over'); $resolve(2);
    });
})->finally(function(){var_dump('promise finally');})->then(function($response){
    var_dump('promise 3 enter val is: '.$response);
});

// defer example
(function(){
    $defer = Promise::defer();
    Go(function()use($defer){ Co::sleep(1); $defer->resolve(1); });
    return $defer->promise;
})()->then(function(){ var_dump('success'); });

// array promise example
Promise::all([
    new Promise(function($resolve, $reject) { Swoole\Timer::after(100, function() use ($resolve){ $resolve(1); }); }),
    new Promise(function($resolve, $reject) { Swoole\Timer::after(100, function() use ($resolve){ $resolve(2); }); }),
    new Promise(function($resolve, $reject) { Swoole\Timer::after(100, function() use ($resolve){ $resolve(3); }); }),
])->then(function($response){
    var_dump($response);
});

Promise::race([
    new Promise(function($resolve, $reject) { Swoole\Timer::after(100, function() use ($resolve){ $resolve(1); }); }),
    new Promise(function($resolve, $reject) { Swoole\Timer::after(100, function() use ($reject){ $reject(2); }); }),
    new Promise(function($resolve, $reject) { Swoole\Timer::after(100, function() use ($resolve){ $resolve(3); }); }),
])->then(function($response){
    var_dump($response);
});

Promise::pipe([
        function($resolve, $reject, $response) {
            var_dump("pipe 1: ". $response); Co::sleep(1); return $resolve(1);
        },

        function($resolve, $reject, $response) {
            var_dump("pipe 2: ". $response); Co::sleep(1); return $resolve(2);
        },
    ],
        function($response) {
            var_dump("pipe 3: ". $response); Co::sleep(1); return MyPROMISE::resolve(3);
        }
    )->then(function(){
        var_dump('over', func_get_args());
    });
You might also like...
This module aims to validate if the pilot made his flights online on the IVAO and VATSIM networks

SMPirepValidator This module aims to validate if the pilot made his flights online on the IVAO and VATSIM networks SMPirepValidator v.1.0 for phpVMS (

The whmcs payment module for TigoPesa Payment Gateway.

whmcs-tigopesa INSTALLATION INSTRUCTIONS TIGOPESA WHMCS Please follow the instructions below to setup the whmcs-tigopesa gateway module. Download zipp

 Content Fuzzyfyr Module for Magento® 2
Content Fuzzyfyr Module for Magento® 2

Content Fuzzyfyr Module for Magento® 2 The Content Fuzzyfyr module for Magento® 2 fills up empty content fields - and if needed - switches real conten

A Zabbix module to show groups/hosts as a tree under Monitoring -> Hosts Tree menu item
A Zabbix module to show groups/hosts as a tree under Monitoring - Hosts Tree menu item

zabbix-module-hosts-tree Written according to Zabbix official documentation https://www.zabbix.com/documentation/current/manual/modules A Zabbix modul

Upgrade module for PrestaShop

1-Click Upgrade About Upgrade to the latest version of PrestaShop in a few clicks, thanks to this automated method. This module is compatible with all

Magento 2 module to quickly acces products, orders and customer from admin menu

Magento 2 module to quickly access product, order or customer views Introduction The Magento 2 backend can be sluggish. Ever wanted to access a produc

Extract and evolution of the magento2-currency-precision module from the magento2-jp project from @Magento

Currency Precision Module for Magento 2 This module aims to help merchants to manage easily their currency precision in Magento 2. DISCLAIMER Initiall

Rah cache minify - HTML compressor module for rah cache Textpattern CMS plugin

Minify module for rah_cache This is a minify module for rah_cache, a full-page caching solution for Textpattern CMS. Rah_cache_minify will minify HTML

Silverstripe-masquerade - SilverStripe module to allow users to "masquerade" as other users

SilverStripe Masquerade Module About This module is designed to allow an Administrator to "login" as another "Member" without changing their password

Owner
拓荒者
拓荒者
Magento 2 Module Experius Page Not Found 404. This module saves all 404 url to a database table

Magento 2 Module Experius Page Not Found 404 This module saves all 404 urls to a database table. Adds an admin grid with 404s It includes a count so y

Experius 28 Dec 9, 2022
一个支持在 Swoole 或其它非 Workerman 环境,开发 Gateway Worker 的组件。

Workerman Gateway SDK 一个支持在 Swoole 或其它非 Workerman 环境,开发 Gateway Worker 的组件。 支持用 Workerman Gateway 做网关,Swoole 编写业务代码。 安装 composer require yurunsoft/wor

Yurun 6 Nov 18, 2022
Php-gamer - A repo with PHP 8.1, Swoole and Laminas. And of course, Docker in the front.

PHP-Gamer Instructions for run this app: First time $ git clone [email protected]:fatorx/php-gamer.git $ cd php-gamer $ chmod +x docker-build.sh $ chmod

Fabio de Souza 6 Oct 6, 2022
Migrations module for ProcessWire

ProcessDbMigrate Introduction This module is designed to ease the problem of migrating database changes from one PW environment to another.

Mark Evens 9 Nov 3, 2022
WHMCS Payment Gateway Module for Coinify

vrcoinify WHMCS Payment Gateway Module for Coinify Installing guide You should copy all contents from module folder to your WHMCS application folder u

VR DEV TEAM 3 Mar 15, 2022
The swiss army knife for Magento developers, sysadmins and devops. The tool provides a huge set of well tested command line commands which save hours of work time. All commands are extendable by a module API.

netz98 magerun CLI tools for Magento 2 The n98 magerun cli tools provides some handy tools to work with Magento from command line. Build Status Latest

netz98 758 Dec 28, 2022
SilverStripe Garbage Collection Module

SilverStripe Module for defining and processing Garbage Collection on SilverStripe Applications.

Brett Tasker 8 Aug 12, 2022
WHMCS Automation Module For AWS EC2 Instances.

使用方法 把AWSEC2目录直接扔到 WHMCS/modules/servers 下即可 自定义字段 cloudinit (文本框 textarea 在订单页面显示) pem (文本框 textarea 仅管理员可见) data (文本框 textarea 仅管理员可见) 特性 动态IP (关机再开

CoiaPrant 9 Jan 28, 2022
Akaunting module to use employee as a customer

Associate Employee to customer App for Akaunting to associate employee to customer. With this app contacts with the employee type can be used as custo

LibreCode coop 2 Sep 21, 2022
Manifest is a ProcessWire module that bridges between Twig and Webpack.

Manifest is a ProcessWire module that bridges between Twig and Webpack.

Rudy Affandi 4 Nov 12, 2022