From 58c25fa9c4a43ea162322b9a539c1cc19e0ea6dc Mon Sep 17 00:00:00 2001 From: Mathew Heard Date: Wed, 13 Oct 2021 01:04:11 +1100 Subject: [PATCH] deferred timeout method --- index.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/index.js b/index.js index 31db840..557c902 100644 --- a/index.js +++ b/index.js @@ -203,6 +203,21 @@ Q.timeout = function (promise, ms, message = undefined){ return deferred.promise } +Q.deferredTimeout = function(deferred, ms, symbol = undefined){ + if(!symbol) { + symbol = new Error(`Timed out after ${ms} ms`) + } + const timer = setTimeout(()=>{ + deferred.reject(symbol) + }, ms) + + deferred.catch(()=>{}).then(()=>{ + clearTimeout(timer) + }) + + return deferred.promise +} + Q.ninvoke = function(object, method, ...args){ return Q.nfcall(object[method].bind(object), ...args) }