const DEVELOPER_INFO = [
" autor " => " Matheus Johann Araújo " ,
" country " => " Brasil " ,
" state " => " Pernambuco " ,
" date " => " 2022-03-12 "
]; The term callback means past function as a parameter of a function, which will be called by a function. In PHP the callbacks are callable type which means chamable;
The class called Closure is responsible for representing anonymous functions and arrow functions (arrow functions);
callback is usually unnamed functions (anonymous or arrow functions) that are passed by parameter, but nothing prevents a named function from being passed as a parameter!
setInterval(callback, milliseconds) performs the callback function in infinitely informed time. The function returns a UID that can be used in the clearInterval function to remove setInterval from the execution line;
setTimeout(callback, milliseconds) performs the callback function call in the time informed in a unique way. The function returns a UID that can be used in the clearTimeout function to remove the setTimeout from the execution line;
clearInterval(UID) completes the future execution of the setInterval that has the informed UID . The function returns true (finished) or false (did not finish or did not find the scheduled task);
clearTimeout(UID) finalizes the future execution of the setTimeout that has the informed UID . The function returns true (finished) or false (did not finish or did not find the scheduled task).
<?php
// EN-US: Include at the beginning of the first file to be interpreted, on the WEB server use TICK sparingly
// PT-BR: Incluir no início do primeiro arquivo a ser interpretado, no servidor WEB use o TICK com moderação
declare (ticks= 1 );
require_once " lib/code.php " ;
echo " Start " , PHP_EOL ;
$ counter = 1 ;
$ uid = setInterval ( function () use (& $ counter ) {
echo " Counter: " , $ counter ++, PHP_EOL ;
}, 100 );
setTimeout ( function () {
echo " Half of the increments " , PHP_EOL ;
}, 1000 );
setTimeout ( function () use ( $ uid ) {
echo " Stopping the counter " , PHP_EOL ;
clearInterval ( $ uid );
}, 2000 );
echo " Processing... " , PHP_EOL ;
// EN-US: Include after timed calls
// PT-BR: Incluir após chamadas programadas (agendadas)
$ count = workWait ( function () { usleep ( 1 ); });
echo " workRun has been run $ {count} times " , PHP_EOL ;
echo " End " , PHP_EOL ; then(callback) method called when promise is resolved. callback will be executed and will receive as a parameter the value passed on the function resolve ;
catch(callback) Method called when Promise is rejected. callback will be performed and will receive as a parameter the value passed on the reject function;
finally(callback) Method called after a promise is resolved or rejected, which fires the execution of the callback informed as a parameter.
<?php
// EN-US: Include at the beginning of the first file to be interpreted, on the WEB server use TICK sparingly
// PT-BR: Incluir no início do primeiro arquivo a ser interpretado, no servidor WEB use o TICK com moderação
declare (ticks= 1 );
require_once " lib/code.php " ;
echo " Start " , PHP_EOL ;
$ promise = new Promise ( function ( $ resolve , $ reject ) {
$ call = rand ( 0 , 1 ) ? $ resolve : $ reject ;
setTimeout ( function () use ( $ call ) {
$ call ( " message " );
}, 1000 );
});
function info_promise () {
global $ promise ;
echo " > monitor: " , $ promise -> getMonitor (), PHP_EOL ;
echo " > state: " , $ promise -> getState (), PHP_EOL ;
}
info_promise ();
$ promise -> then ( function ( $ result ) {
echo " then ( $ {result} ) " , PHP_EOL ;
info_promise ();
})-> catch ( function ( $ error ) {
echo " catch ( $ {error} ) " , PHP_EOL ;
info_promise ();
})-> finally ( function () {
echo " finally " , PHP_EOL ;
info_promise ();
});
echo " Processing... " , PHP_EOL ;
for ( $ counter = 0 ; $ counter < 10 ; $ counter ++) {
echo " Counter: $ {counter}" , PHP_EOL ;
usleep ( 200000 );
}
// EN-US: Include after timed calls
// PT-BR: Incluir após chamadas programadas (agendadas)
$ count = workWait ( function () { usleep ( 1 ); });
echo " workRun has been run $ {count} times " , PHP_EOL ;
echo " End " , PHP_EOL ;