php_timers_promise_async_await_thread
1.0.0
const DEVELOPER_INFO = [
" autor " => " Matheus Johann Araújo " ,
" country " => " Brasil " ,
" state " => " Pernambuco " ,
" date " => " 2022-03-12 "
];术语callback表示过去的函数作为函数的参数,该函数将通过函数调用。在PHP中, callbacks是callable类型,意为重叠;
称为Closure类负责表示匿名函数和arrow functions (箭头函数);
callback通常是通过参数传递的未命名函数(匿名或箭头函数),但没有任何阻止命名函数作为参数传递!
setInterval(callback, milliseconds)在无限知情的时间内执行回调函数。该函数返回可以在clearInterval函数中使用的UID ,以从执行行中删除setInterval 。
setTimeout(callback, milliseconds)在以独特的方式通知的时间内执行回调函数调用。该函数返回可以在clearTimeout函数中使用的UID ,以从执行行中删除setTimeout ;
clearInterval(UID)完成具有知情UID的setInterval的未来执行。功能返回true (完成)或false (未完成或找不到计划的任务);
clearTimeout(UID)最终确定了具有知情UID的setTimeout的未来执行。该功能返回true (完成)或false (未完成或找不到计划的任务)。
<?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)方法在解决承诺时调用。 callback将被执行,并将作为参数接收到函数resolve上传递的值;
当承诺被拒绝时, catch(callback)方法。将执行callback ,并将作为参数接收到reject函数上传递的值;
finally(callback)方法在解决或拒绝承诺后调用,这解散了被告知作为参数的callback的执行。
<?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 ;