Last active
December 17, 2021 02:06
-
-
Save john-doherty/bcf35d39d8b30d01ae51ccdecf6c94f5 to your computer and use it in GitHub Desktop.
Revisions
-
John Doherty revised this gist
Nov 20, 2019 . 1 changed file with 106 additions and 32 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,33 +1,107 @@ (function () { 'use strict'; /** * wraps a promise in a timeout, allowing the promise to reject if not resolve with a specific period of time * @param {integer} ms - milliseconds to wait before rejecting promise if not resolved * @param {Promise} promise to monitor * @example * promiseTimeout(1000, fetch('https://courseof.life/johndoherty.json')) * .then(function(cvData){ * alert(cvData); * }) * .catch(function(){ * alert('request either failed or timed-out'); * }); * @returns {Promise} resolves as normal if not timed-out, otherwise rejects */ function promiseTimeout(ms, promise) { return new Promise(function (resolve, reject) { // create a timeout to reject promise if not resolved var timer = requestTimeout(function () { reject(new Error('Promise Timed Out')); }, ms); promise.then(function (res) { clearRequestTimeout(timer); resolve(res); }) .catch(function (err) { clearRequestTimeout(timer); reject(err); }); }); } /* #region helpers */ var requestAnimFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(callback) { window.setTimeout(callback, 1000 / 60); }; /** * Behaves the same as setTimeout except uses requestAnimationFrame() where possible for better performance * @param {function} fn The callback function * @param {int} delay The delay in milliseconds * @returns {object} handle to the timeout object */ function requestTimeout(fn, delay) { if (!window.requestAnimationFrame && !window.webkitRequestAnimationFrame && !(window.mozRequestAnimationFrame && window.mozCancelRequestAnimationFrame) && // Firefox 5 ships without cancel support !window.oRequestAnimationFrame && !window.msRequestAnimationFrame) return window.setTimeout(fn, delay); var start = new Date().getTime(); var handle = {}; var loop = function() { var current = new Date().getTime(); var delta = current - start; if (delta >= delay) { fn.call(); } else { handle.value = requestAnimFrame(loop); } }; handle.value = requestAnimFrame(loop); return handle; } /** * Behaves the same as clearTimeout except uses cancelRequestAnimationFrame() where possible for better performance * @param {object} handle The callback function * @returns {void} */ function clearRequestTimeout(handle) { if (handle) { window.cancelAnimationFrame ? window.cancelAnimationFrame(handle.value) : window.webkitCancelAnimationFrame ? window.webkitCancelAnimationFrame(handle.value) : window.webkitCancelRequestAnimationFrame ? window.webkitCancelRequestAnimationFrame(handle.value) : /* Support for legacy API */ window.mozCancelRequestAnimationFrame ? window.mozCancelRequestAnimationFrame(handle.value) : window.oCancelRequestAnimationFrame ? window.oCancelRequestAnimationFrame(handle.value) : window.msCancelRequestAnimationFrame ? window.msCancelRequestAnimationFrame(handle.value) : clearTimeout(handle); } } /* #endregion */ if (typeof window === 'undefined') { module.exports = promiseTimeout; } else { window.promiseTimeout = promiseTimeout; } })(); -
John Doherty renamed this gist
Aug 17, 2017 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
John Doherty revised this gist
Oct 9, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ /** * wraps a promise in a timeout, allowing the promise to reject if not resolve with a specific period of time * @param {integer} ms - milliseconds to wait before rejecting promise if not resolved * @param {Promise} promise to monitor * @Example -
John Doherty revised this gist
Oct 9, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ /** * wraps a promise in a timeout, allowing the promise to timeout if not resolve with a specific period of time * @param {integer} ms - milliseconds to wait before rejecting promise if not resolved * @param {Promise} promise to monitor * @Example -
John Doherty revised this gist
Oct 7, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -11,7 +11,7 @@ * alert('request either failed or timedout'); * }); */ function promiseTimeout(ms, promise){ return new Promise(function(resolve, reject){ -
John Doherty renamed this gist
Oct 7, 2016 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
John Doherty created this gist
Oct 7, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,33 @@ /** * @description adds a timeout * @param {integer} ms - milliseconds to wait before rejecting promise if not resolved * @param {Promise} promise to monitor * @Example * promiseTimeout(1000, fetch('https://courseof.life/johndoherty.json')) * .then(function(cvData){ * alert(cvData); * }) * .catch(function(){ * alert('request either failed or timedout'); * }); */ function timeoutPromise(ms, promise){ return new Promise(function(resolve, reject){ // create a timeout to reject promise if not resolved var timer = setTimeout(function(){ reject(new Error("promise timeout")); }, ms); promise .then(function(res){ clearTimeout(timer); resolve(res); }) .catch(function(err){ clearTimeout(timer); reject(err); }); }); };