Skip to content

Unable to run dataloader for simple example #29

Open
@yahya-uddin

Description

@yahya-uddin

I am really struggling to create even a simple version of this using the documentation.

Here is my code:

use GuzzleHttp\Promise\Promise;
use Overblog\DataLoader\DataLoader;
use Overblog\PromiseAdapter\Adapter\GuzzleHttpPromiseAdapter;

class Sandbox
{
    public function handle()
    {
        $myBatchGetUsers = function ($keys) {
            echo "Running myBatchGetUsers()...\n";
            $promise = new Promise();
            $promise->then(function ($value) {
                echo "Running data promise..."; // never runs!
                return [
                    ['name' => 'John'],
                    ['name' => 'Sara'],
                ];
            });
            return $promise;
        };

        $promiseAdapter = new GuzzleHttpPromiseAdapter();
        $userLoader = new DataLoader($myBatchGetUsers, $promiseAdapter);

        $userLoader->load(4)
            ->then(function ($user) use ($userLoader) {
                echo "{$user['name']}\n"; // never runs
            });

        $userLoader->load(5)
            ->then(function ($user) use ($userLoader) {
                echo "{$user['name']}\n"; // never runs
            });

        $userLoader->await();
    }
}

The expected output is:

Running myBatchGetUsers()...
Running data promise...
John
Sara

However the actual output is:

Running myBatchGetUsers()...

As you can see it is a very simple example, yet it does not work.
What am I doing wrong?

Many Thanks :-)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions