Fork Copy function executeWithTimeout(timeout, executor) { if (arguments.length == 1) { return function (exec) { return executeWithTimeout(timeout, exec); } } return new Promise((resolve, reject) => { let isTimedOut = false; let timer = setTimeout(() => { isTimedOut = true; reject(new Error('Timed out')); }, timeout); executor() .then(response => { if (!isTimedOut) { clearTimeout(timer); resolve(response); } }).catch(reject); }); } executeWithTimeout(4000, function() { return fetch('https://webhook.site/7c854640-e7eb-4c13-b9df-e945fe839e82', { mode: 'cors' }); }) .then(res => res.text()) .then(console.log) .catch(console.error);