Comments (8)
Some help please me before i start trying to reinvent the promise wheel...!
from promise.
Hi @ozeals, welcome to the wonderful world of promises!
What you're seeing is expected behavior, the promise will "follow" the given promise and the child promise will automatically settle when the parent promise settles. See also https://github.com/reactphp/promise#deferredresolve.
The way I understand your code it means that you should simply await the resolution value instead of waiting for a promise to resolve with another promise. In other words, instead of this
$defered->promise()->then(function($promise){
$defered = new Deferred();
$promise->then(function($databasevalue) use ($defared) {
$proccessedlogentries = 'proccessed-log-entries';
$defered->resolve($proccessedlogentries);
});
return $defered->promise();
})->then(function($logentriespromise){
//... blah blah blah
});
do this instead:
$defered->promise()->then(function ($databasevalue) {
$proccessedlogentries = 'proccessed-log-entries';
return $processedlogentries;
})->then(function($logentriesvalue){
//... blah blah blah
});
I hope this helps 👍
from promise.
Thanks @clue for the enlightenment.. But that would mean the Promise works exactly like a pipe line.. right!!. okay say i want to resolve proccessedlogentries
on the event loop like
$defered->promise()->then(function($promise) use ($loop) {
$defered = new Deferred();
$loop->furturTick(function() use ($defered){
$proccessedlogentries = 'proccessed-log-entries';
$defered->resolve($proccessedlogentries);
});
return $defered->promise();
})->then(function($logentriespromise){
// this then function is never called
//... blah blah blah
})->then(function(){
// this then function is never called too
});
I was thinking of trying this
class PromiseWrapper
{
proctected $wraped;
public function __construct($promise)
{
$this->wraped = $promise;
}
public function promise()
{
return $this->wraped;
}
public static function wrap($promise)
{
return new static($promise);
}
}
resolve
$done = new Promise(function($resolve){
$somedatabasevalue = 'database-value';
$resolve($somedatabasevalue);
});
$defered->resolve(PromiseWraper::wrap($done));
but it seams kinda hacky
from promise.
The unwraping of the promises makes prefect sence but Promise will be EXACTLY like a pipeline.. instead of being able to defer deferent steps of the then execution to something like an event loop...
[resolve-promise]->then[sched on futureTick]->then[shed on futureTick]->then[results]
That will make more sence for unwraping the promise and be truely async.
from promise.
@ozeals I'm not sure I follow exactly what you're trying to achieve here, but your first example looks almost good. It looks like you've misspelled $loop->futureTick()
in there which will throw a Throwable
on PHP7+ which will reject your promise. This explains why your resolution handlers will not be called anymore. You should add a rejection handler (second argument to then), for testing purposes I often play around with this like this:
$promise->then('var_dump', 'printf');
I'm not sure what you mean by your pipeline analogy, but I guess it might make sense if you prefer to view it as such.
from promise.
@clue sry about the typo.. My goal is resolve then resolver on the event loop like
$defered->promise()->then(function($promise) use ($loop) {
$defered = new Deferred();
$loop->futurTick(function() use ($defered){
$proccessedlogentries = 'proccessed-log-entries';
$defered->resolve($proccessedlogentries);
});
return $defered->promise();
})->then(function($logentriesresults) use ($loop) {
$defered = new Deferred();
$loop->futurTick(function() use ($defered, $logentriesresults){
$longtask = "done some other long taks with [$logentriesresults]";
$defered->resolve($longtaks);
});
return $defered->promise();
})->then(function(){
// this then function is never called too
});
Say each then function takes a long time to run.. Having them all run syncronosly in series when the first promise is resolved is not ideal for me. But having each then function shedule some futureTick task on the event loop then resolve its results to the next then function is true async. I don't care how long each then function takes once its starts i just dont want them running synchronously in series after the first resolve but rather asynchronously in series
from promise.
Thanks for the help @clue.
from promise.
Your last example still has a typo that will cause this Throwable
, perhaps try it like this:
$defered->promise()->then(function($promise) use ($loop) {
$defered = new Deferred();
$loop->futureTick(function() use ($defered){
$proccessedlogentries = 'proccessed-log-entries';
$defered->resolve($proccessedlogentries);
});
return $defered->promise();
})->then(function($logentriesresults) use ($loop) {
$defered = new Deferred();
$loop->futureTick(function() use ($defered, $logentriesresults){
$longtask = "done some other long taks with [$logentriesresults]";
$defered->resolve($longtaks);
});
return $defered->promise();
})->then(function(){
// this then function is never called too
})->then('var_dump', 'printf'); // will dump the results or print any exception
The above code should work or at least help you diagnose why it doesn't. That being said, it sounds like promises may not be the best tool for what you're trying to achieve. Instead of breaking a blocking application into smaller parts, have you considered going fully non-blocking? This works for pretty much any I/O operation, such as HTTP or database calls etc., but less so for CPU intensive operations (which most PHP applications do not tend to be).
from promise.
Related Issues (20)
- 2.x returned promise types HOT 3
- Fullfilled or Rejected promise and then method HOT 9
- Is there is any way in Reactphp to run code really asynchronously HOT 1
- [RFC] Consider deprecating FulfilledPromise and RejectedPromise (mark as internal only) HOT 6
- Detecting thenable causes unwanted side effects
- ETA version 3? HOT 4
- Call promise twice HOT 2
- When serializing & later unserializing exceptions thrown by clashing function calls HOT 13
- PHP 8.0 Deprecation Notice HOT 1
- Blocking a promise from running untill another promise has completed. HOT 11
- Problem with PHP 8 HOT 3
- allSettled operator HOT 2
- QUESTION - How to test if a code is blocking or not HOT 2
- php 7.1 multiple exceptions using the pipe (|) character HOT 1
- Allow `iterable` instead of `array` for `all()`, `race()` and `any()` HOT 1
- Add example folder HOT 2
- Support Disjunctive Normal Form Types (DNF types) for PHP 8.2+
- Exception: Value of type null is not callable HOT 5
- Arguments expecting callables should describe the full signature of callables HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from promise.