promise timeout code example
Example 1: promise settimeout
await new Promise(resolve => setTimeout(resolve, 1000))
Example 2: promise with timeout js
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, };
};