0

I'm working on something that script need to wait for do childFunc and after that return the result of childFunc and after that, the script continue. Something like async and await in javascript.

<?php

$loginResponse = $system->login($username, $password);

        if ($loginResponse !== null && $loginResponse->isTwoFactorRequired()) {

            $twoFactorIdentifier = $loginResponse->TwoFactorInfo()->getTwoFactorIdentifier();


            // I need to wait here that myChaildFucn LoadView and get data from user and then after some process retuen result!!!! 

            $verificationCode = myChaildFucn();

            $system->checkTwoFactorLogin($username, $password, $twoFactorIdentifier, $verificationCode,);
        }
Mohsen Rasouli
  • 352
  • 3
  • 9
  • 22
  • Your script will __always__ wait for result of `myChaildFucn`. – u_mulder Apr 11 '20 at 21:02
  • also read here: [How do I implement a callback in PHP?](https://stackoverflow.com/questions/48947/how-do-i-implement-a-callback-in-php) – Vickel Apr 11 '20 at 21:03

1 Answers1

0

Salaam,

(if you want to speak Persian, I'm okay but I prefer to answer by English to ensure other people who has same your problem can find the solution)

so as you know, PHP is a synchronous engine and we have to use ajax to split our requests or use some functions to simulate Asynchronous performances.

anyway...

we have two options to solve this issue generally:

  1. use sleep function : if you are sure about your delay timing, so just use it and define a delay to stop your app for seconds, otherwise It's not a good idea for your situation.

  2. use foxyntax_ci in https://github.com/LegenD1995/foxyntax_ci : if you have SPA or using REST API, It's your best option!! actually it has some libraries on codeigniter and help you to build your queries, working with files, session, cookies, config authorization and ... with Asynchronous function from javascript. (NOTE: It's Alpha version but it hasn't bugs, just needs to review in media function to improve performance).

Milad Mohammadi
  • 176
  • 1
  • 9