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