settimeout promise

setTimeout(function(){
 	alert("Sup!"); 
}, 2000);//wait 2 seconds
 
 
 
settime out with promise
Javascript By Marwen Labidi on Feb 22 2022
const sleep = (time) => new Promise((resolve) => setTimeout(resolve, time));

await sleep(500)
// Do something after the sleep!

 
 
 
promise with timeout js
Javascript By Maximauve_ on Jun 1 2021 Donate
const promiseWithTimeout = promise => {
  let timeoutId;   
  const timeoutPromise = new Promise((_, reject) => {
    timeoutId = setTimeout(() => {      
      reject(new Error('Request timed out'));    
    }, 4000);  })
  return {
    promiseOrTimeout: Promise.race([promise, timeoutPromise]),
    timeoutId,  };
};

posted on 2022-07-07 11:49  漫思  阅读(57)  评论(0编辑  收藏  举报

导航