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 ;