Giter Site home page Giter Site logo

Comments (11)

JABirchall avatar JABirchall commented on July 24, 2024 1

I just read over the fibres RFC, and yea looks pretty much onpoint with what we currently needed. Nice to see PHP get some engine level async functionality.

from promise.

JABirchall avatar JABirchall commented on July 24, 2024

I cant share specific code because its not my property to share. But here is a cut down nature of what we are attempting.

    public function __get($name)
    {
        if ($name === "something") {
            $repository = RepoManager::get('repo');
            $repository->setSomething($this->something->id);
            $data = $repository->getById($this->id);
            if($data === null) {
                $repository->api->get($this->id); // Creates the promise to fetch the data from the API and store in the repository
                $loop = DependencyManager::get('Loop');
               
                while($data = $repository->getById($this->id) === null) { // Idea to run the loop until our promise has completed and data exists?
                    $loop->moveForward();  // run the loop once until it is completed our required promise?
                }
            }
            
            return $data;
        }
    }

We need a way to run $repository->api->get($this->id) to completion inline our current promise so we can use the data.

from promise.

WyriHaximus avatar WyriHaximus commented on July 24, 2024

What you can do is chain the promises:

$this->db->operationA()->then(fn () => $this-db->operationB());

This ensures your second operation isn't started before the first has completed.

from promise.

JABirchall avatar JABirchall commented on July 24, 2024

What you can do is chain the promises:

$this->db->operationA()->then(fn () => $this-db->operationB());

This ensures your second operation isn't started before the first has completed.

How would that work down the call tree?
Because this is being called as a normal __get. Expecting the $data to be a value not a promise.

$object->something with the expectation ->something will be the data relative to the object.
The $object is getting passed down a promise chain and ->something is being accessed conditionally
Sometimes it might get called, sometimes it might not, and sometimes we might already have the data loaded and other times not.

The database is a Redis server which we don't wrap in promises, we are trying to keep promises down to a minimal needed use case, for example the API calls is a promise. Redis data isn't as it is fast enough for our use.

from promise.

HLeithner avatar HLeithner commented on July 24, 2024

To my current understanding of reactphp what you like to achieve is against the principals of reactPHP and the promise approach.

If you want to have a function blocking and always return a value then you can't run async methods within the function. You have to return a promise.

You can't tell reactPHP "please do the next promise in queue and come back after it is solved and ask me again".

ymmv

from promise.

JABirchall avatar JABirchall commented on July 24, 2024

To my current understanding of reactphp what you like to achieve is against the principals of reactPHP and the promise approach.

If you want to have a function blocking and always return a value then you can't run async methods within the function. You have to return a promise.

You can't tell reactPHP "please do the next promise in queue and come back after it is solved and ask me again".

ymmv

Thanks, that's a shame, because that's what we exactly need. I get what you say, but its also not blocking per say, as I want to direct execution to another promise chain until its resolved, same to how JS await works.

I tried this library, https://github.com/adrianmihaila/reactphp-promise-wait but that don't work with newer versions of reactphp.

from promise.

WyriHaximus avatar WyriHaximus commented on July 24, 2024

Thanks, that's a shame, because that's what we exactly need. I get what you say, but its also not blocking per say, as I want to direct execution to another promise chain until its resolved, same to how JS await works.

Then you're going to love fibers coming in PHP 8.1. With those most of our public API is likely to change. Until than you can use https://github.com/recoilphp/recoil

I tried this library, https://github.com/adrianmihaila/reactphp-promise-wait but that don't work with newer versions of reactphp.

Would suggest using https://github.com/clue/reactphp-block instead. But keep in mind it's a hack to keep starting and stopping the loop.

from promise.

JABirchall avatar JABirchall commented on July 24, 2024

Would suggest using https://github.com/clue/reactphp-block instead. But keep in mind it's a hack to keep starting and stopping the loop.

Thank you, this works perfectly.

I see what you mean by starting/stopping the loop.
I had to wrap my $loop->run() in a while loop to keep the application alive.

Then you're going to love fibers coming in PHP 8.1. With those most of our public API is likely to change.

So will this come with a an await feature?

Our current targeted version is 7.4 with PHP8 support, I doubt we would move over to only PHP 8.1 support so soon, we want to keep some BC

from promise.

WyriHaximus avatar WyriHaximus commented on July 24, 2024

Would suggest using clue/reactphp-block instead. But keep in mind it's a hack to keep starting and stopping the loop.

Thank you, this works perfectly.

I see what you mean by starting/stopping the loop.
I had to wrap my $loop->run() in a while loop to keep the application alive.

Yes, that is the price you're paying for doing it this way. Which is something we're aiming to solve with fibers in 8.1.

Then you're going to love fibers coming in PHP 8.1. With those most of our public API is likely to change.

So will this come with a an await feature?

Our current targeted version is 7.4 with PHP8 support, I doubt we would move over to only PHP 8.1 support so soon, we want to keep some BC

In a nutshell yes. Behind the scenes, it's a lot more complicated but it will let you write that example of yours a bit up like this, without any promises insight:

    public function __get($name)
    {
        if ($name === "something") {
            $repository = RepoManager::get('repo');
            $repository->setSomething($this->something->id);
            return $repository->getById($this->id);
        }
    }

We might be able to provide a similar API on an earlier PHP version but we aren't sure yet if that is possible and feasible. But yeah I get your concern, for us it would main bumping the minimum PHP version requirement of this project from 5.3 (yes it all still works on that version (I know it's crazy don't ask)) to 8.1.

from promise.

SimonFrings avatar SimonFrings commented on July 24, 2024

Hey @JABirchall, I don't know if I'm too late to the party but PHP 8.1 is out and fibers are a thing now!

With that said you can also check out the new react/async package that has been created in addition to PHP's new release. It's still a development version but we're not far from a stable version. ^^

from promise.

clue avatar clue commented on July 24, 2024

I believe this has been answered, so I'm closing this for now. Please come back with more details if this problem persists and we can always reopen this 👍

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.