Skip to content

Instantly share code, notes, and snippets.

@john-doherty
Last active December 17, 2021 02:06
Show Gist options
  • Select an option

  • Save john-doherty/bcf35d39d8b30d01ae51ccdecf6c94f5 to your computer and use it in GitHub Desktop.

Select an option

Save john-doherty/bcf35d39d8b30d01ae51ccdecf6c94f5 to your computer and use it in GitHub Desktop.

Revisions

  1. John Doherty revised this gist Nov 20, 2019. 1 changed file with 106 additions and 32 deletions.
    138 changes: 106 additions & 32 deletions javascript-promise-timeout.js
    Original file line number Diff line number Diff line change
    @@ -1,33 +1,107 @@
    /**
    * 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 timedout');
    * });
    */
    function promiseTimeout(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);
    (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;
    }

    })();
  2. John Doherty renamed this gist Aug 17, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. John Doherty revised this gist Oct 9, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion promise-timeout.js
    Original 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
    * 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
  4. John Doherty revised this gist Oct 9, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion promise-timeout.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    /**
    * @description adds a timeout
    * 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
  5. John Doherty revised this gist Oct 7, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion promise-timeout.js
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@
    * alert('request either failed or timedout');
    * });
    */
    function timeoutPromise(ms, promise){
    function promiseTimeout(ms, promise){

    return new Promise(function(resolve, reject){

  6. John Doherty renamed this gist Oct 7, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  7. John Doherty created this gist Oct 7, 2016.
    33 changes: 33 additions & 0 deletions promise-timeout
    Original 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);
    });
    });
    };