await new Promise(resolve => setTimeout(resolve, 1000))
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, };
};