Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

larafam's avatar

async in php

im currently really interested with async world, i want to move into nodejs async world, but then im too lazy to learn new stuff and stick with php (laravel).. can some one give me an async use case in php and how we tackle it with php? im thinking using spatie/async or reactphp..

0 likes
6 replies
NickVahalik's avatar

No matter how you move forward you're going to have to learn to retrain your brain when you think about how Async works.

If you've been doing PHP, then you're likely used to code running in a predictable, sequential manner.

This is not how things work in the async world. Instead, functions can and will execute in whatever order they finish in.

If you're new to Async at all, I'd recommend spending some time reading up on Promises and get your head wrapped around them. I really like this site which provide visualizations for how things work.

Now, at a technical level, I'm no expert in the how of PHP's async stuff. However, it looks like spatie/async uses PHP's process control functions to actually handle the async code snippets. Not sure what React PHP does here.

Ultimately, Promises are still the general way of how most async stuff works. Even ReactPHP's react/promise class is just a port of Promise/A+ from ES6.

Anyway, the general idea is this: Consider what you're needing to do, and what you need in order to accomplish that task, and then write small, chainable blocks of code that take each other has inputs.

larafam's avatar

@NickVahalik so if i want to send 100 email, i can just do it like this

// SomeController

$pool->add(new SendHundredEmail());

return back();

right?

Cronix's avatar

You'd want to use queues for that. Queues are async if you don't use the file driver and use redis or the database. It just sends jobs to the queue and lets the queue process it in the background and not waiting for a response. It just continues on with regular flow.

larafam's avatar

@Cronix so what is the difference between queue worker and async stuff provide by spatie/async or reactphp library?

Cronix's avatar

I haven't used either of those 2 packages/libraries.

NickVahalik's avatar

@larafam @Conrix is right. You'd use Queues for that. But let's say that you're implementing some functionality that takes some tweets, loads the top 5 tweets of those tweeters and then spits them out. It might looks something like:

loadLastFiveTweetsFromFeed()
    -> then(function ($results) {
        $lastFiveTweetsOfEach = [];
        foreach ($results as $result) {
            $lastFiveTweetsOfEach[] = loadLastFiveTweetsFromUser($result->user); // Assuming this returns a Promise itself... 
    // each of these would load asynchronously.
        }
        return Promise.all($lastFiveTweetsOfEach); // Continue once all promises return.
    })
    ->then(function ($results) {
        // You now have all of the tweets loaded.
    });

Please or to participate in this conversation.