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 }