From 465a5049c24cd5b74add0961d1ad32f3ed423f52 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Sun, 2 Aug 2020 13:50:14 -0400 Subject: [PATCH] timeout2 routine --- services/gunDB/contact-api/utils/index.js | 24 +++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/services/gunDB/contact-api/utils/index.js b/services/gunDB/contact-api/utils/index.js index 81b12fad..a5951e16 100644 --- a/services/gunDB/contact-api/utils/index.js +++ b/services/gunDB/contact-api/utils/index.js @@ -70,6 +70,29 @@ const timeout5 = promise => { ]) } +/** + * @template T + * @param {Promise} promise + * @returns {Promise} + */ +const timeout2 = promise => { + /** @type {NodeJS.Timeout} */ + // @ts-ignore + let timeoutID + return Promise.race([ + promise.then(v => { + clearTimeout(timeoutID) + return v + }), + + new Promise((_, rej) => { + timeoutID = setTimeout(() => { + rej(new Error(Constants.ErrorCode.TIMEOUT_ERR)) + }, 2000) + }) + ]) +} + /** * @template T * @param {(gun: GUNNode, user: UserGUNNode) => Promise} promGen The function @@ -330,5 +353,6 @@ module.exports = { mySecret, promisifyGunNode: require('./promisifygun'), timeout5, + timeout2, isNodeOnline }