1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Returns value use AsyncTask

Discussion in 'Development' started by Akmal, Jun 27, 2019.

  1. Akmal

    Akmal Spider

    Messages:
    10
    GitHub:
    AkmalFairuz
    It's possible? To return value use AsyncTask example:
    PHP:
    public function getCredits() : int {
            
    Server::getInstance()->getAsyncPool()->submitTask(new AsyncTask());
            
    // return $value;
    }
     
  2. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    "Async" by definition cannot return a value.
    Using this answer as a reference: https://stackoverflow.com/a/748189/3990767
    The next line of code is executed before the async task finishes. Therefore, the result is not available when the next line is run.
    Therefore, classical getters cannot be achieved if the backend is async. Instead, the caller should pass a Closure into the getter, and the getter passes the Closure to the AsyncTask, which will call the closure when the data are ready.

    Several answers in this StackOverflow thread have excellent elaboration.
    See also https://github.com/SOF3/forums-comm...t-is-threading-does-it-make-the-server-faster
     
    HimbeersaftLP and Muqsit like this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.