How to use a single Promise in v3?
xpader opened this issue · comments
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.
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.
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.
After some learning, I found that the best way is still async() and driver->run(), I now have a different understanding.