</p> <p>fungsi setTimeoutReturnPromise(delay) {<br> return new Promise((resolve ) => {<br></p> <div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">setTimeout(resolve, delay);
}
<p>async(function() {<br> setTimeoutReturnPromise(5000).then( () => {<br></p> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">console.log("async called back");
});
<p>fungsi kemudian(delay) {<br> return new Promise((resolve) => {<br></p> <div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">setTimeout(resolve, delay);
}
const kemudian = (kelewatan, nilai) => Janji baharu(menyelesaikan => setTimeout(menyelesaikan, menangguhkan, nilai));
const kemudian = (kelewatan, nilai) => {
biarkan pemasa = 0;
biarkan tolak = null;
const promise = new Promise((resolve, _reject) => {
reject = _reject; timer = setTimeout(resolve, delay, value);
kembali {
get promise() { return promise; }, cancel() { if (timer) { clearTimeout(timer); timer = 0; reject(); reject = null; } }
};
Atas ialah kandungan terperinci Bagaimana untuk Mencipta dan Mengurus Janji Menggunakan setTimeout dalam JavaScript?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!