Code Monkey home page Code Monkey logo

Comments (8)

ozeals avatar ozeals commented on May 20, 2024

Some help please me before i start trying to reinvent the promise wheel...!

from promise.

clue avatar clue commented on May 20, 2024

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.

ozeals avatar ozeals commented on May 20, 2024

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.

ozeals avatar ozeals commented on May 20, 2024

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.

clue avatar clue commented on May 20, 2024

@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.

ozeals avatar ozeals commented on May 20, 2024

@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.

ozeals avatar ozeals commented on May 20, 2024

Thanks for the help @clue.

from promise.

clue avatar clue commented on May 20, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.