settimeout promise
setTimeout(function(){
alert("Sup!");
}, 2000);//wait 2 seconds
const sleep = (time) => new Promise((resolve) => setTimeout(resolve, time));
await sleep(500)
// Do something after the sleep!
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, };
};
漫思