Ustawa o obietnicy
const sleep = (time) => new Promise((resolve) => setTimeout(resolve, time));
await sleep(500)
// Do something after the sleep!
Marwen Labidi
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, };
};