Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use await inside a class function? #62

Closed
Parsoolak opened this issue Oct 18, 2021 · 4 comments
Closed

How to use await inside a class function? #62

Parsoolak opened this issue Oct 18, 2021 · 4 comments
Labels

Comments

@Parsoolak
Copy link

I'm having some difficulty using Block\await function in reactphp codes. I implemented this issue in 3 ways but either way it failed to do my task!

Number 1

It will execute correctly but it close the connection and stops server

public function awaitLottieResizer($amount, $asset)
    {
        try {
            $fun =  aPromiseFunction(); // it will return PromiseInterface
            return await($fun);
        } catch (\Exception $e) {
            return "Exception";
        }
    }

Number 2

It won't return anything and code stuck but the server is running

public function awaitLottieResizer($amount, $asset)
    {
        try {
            $fun =  aPromiseFunction(); // it will return PromiseInterface
            return await($fun, null);
        } catch (\Exception $e) {
            return "Exception";
        }
    }

Number 3

It won't return anything and code stuck but the server is running

public function awaitLottieResizer($amount, $asset)
    {
        try {
            $fun =  aPromiseFunction(); // it will return PromiseInterface
            return await($fun, React\EventLoop\Loop::get());
        } catch (\Exception $e) {
            return "Exception";
        }
    }
@Parsoolak
Copy link
Author

PS: I use React\Http\HttpServer; to create my rest api

@Parsoolak
Copy link
Author

PROGRESS REPORT:
I Found the issue
When I Call the awaitLottieResizer function from a promise interface, Ill get the response but when i call it from out of a promise, I Dont Get any response
also when i call it from inside a promise, my inner promise stop and it closed the server

  private function processTradesAndApply($balanceArray, $userId): PromiseInterface
    {
       // Not Work Here
        $S = $this->awaitLottieResizer(100, "IRT");
        
        return $this->appManagerDatabase
            ->createSelectQuery(
                "Trades",
                ['user' => $userId]
            )->then(function ($result) use ($balanceArray, $userId) {
        // Work Here But Stops the Loop
        $S = $this->awaitLottieResizer(100, "IRT");
    };

}

@Parsoolak
Copy link
Author

a Better Question:
How to wait for promise inside foreach loop in reactphp?

@clue clue added the question label Oct 20, 2021
@clue
Copy link
Owner

clue commented Oct 20, 2021

@Parsoolak Thanks for bringing this up! This has been discussed in #20, #44, #49 and others before and this is more or less by design.

As a rule of thumb, mixing blocking and non-blocking code should be avoided. This means these functions should only be used in a blocking application. If you're using a non-blocking application, you should use non-blocking primitives provided by promises and event emitters.

If you're building an HTTP application, here's an example how this can be used with Framework X:
https://gist.github.com/clue/c6f92f48f36ec5061ecf12dde6729a61

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 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants