amphp / amp

A non-blocking concurrency framework for PHP applications. 🐘

Home Page:https://amphp.org/amp

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to use a single Promise in v3?

xpader opened this issue · comments

commented

Just a simple example:

<?php

use function Amp\async;
use function Amp\delay;

require __DIR__.'/vendor/autoload.php';

async('Amp\delay', 3000)->onResolve(function() {
    print("After delay 3000\n");
});

This code has no any output..

async('Amp\delay', 3000)->onResolve(function() {
    print("After delay 3000\n");
});

delay(2000);
echo "After delay 2000\n";

This has 2000 and 3000 delay output.

async('Amp\delay', 3000)->onResolve(function() {
    print("After delay 3000\n");
});

Loop::getDriver()->run();

This has output.

The question is how to use a simple Promise with callback? Not coroutine synchronous writing

There won't be callbacks in v3. Either you await or you need to use run() as you did.

commented

That's weird, Fiber support writing code as synchronous, but look's like force to use synchronous style, sometimes let concurrent programming become difficult, sometimes we need callback, this makes many callback style invalid.

Where do you need promise callbacks? You can always start a new fiber and invoke your callback there.

commented

See my other issue: #352
I can't make amp v3 compatible with traditional asynchronous framework because they use callback.
The synchronous code block whole stack.
I think may be the question is Event-Loop.

commented

After some learning, I found that the best way is still async() and driver->run(), I now have a different understanding.