codeigniterreactphp

How to load a CodeIgniter view in $promise->then() in a controller?


In my CodeIgniter 2 controller I call a model method which returns a ReactPHP promise, and I want to load a CodeIgniter view in the function called by that promise's ->then() method. How can I do this? What happens instead is the controller method returns nothing, so I get a blank page in the browser.

Here is a simplified example illustrating what I'm trying to do:

class My_class extends My_Controller {

    function my_method() {

        $this->my_model->returns_a_promise()->then(function ($data) {

            // How can I pass the promise's resolved value to the template here?
            // It seems this never gets called, because my_method() returns 
            // before we get here. :(
            $this->load->view('my_view', $data);

        });

    }

}

Is there any way to tell the controller method not to send output to the browser until after the promise has resolved?


Solution

  • It turns out the code in my original question does work. So the question is the answer. But the reason it wasn't working for me was that returns_a_promise() was not returning a resolved promise, so ->then() was not called and the view was not rendered. In order to make it return a resolved promise, I had to call $deferred->resolve(); in the code that returned the promise.

    The upshot of this is that this code example demonstrates it is possible to run asynchronous PHP (via ReactPHP in this case) in CodeIgniter controller methods. My particular use case is to run many database queries concurrently in the CodeIgniter model.