From c8883b55d14c2f4fcfc11cb4ab7073177e415da1 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Wed, 3 Feb 2021 10:31:57 -0400 Subject: [PATCH] use on(), wait 10 seconds for epub to arrive --- services/gunDB/contact-api/utils/index.js | 30 +++++++---------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/services/gunDB/contact-api/utils/index.js b/services/gunDB/contact-api/utils/index.js index bcab3105..fc2abcd3 100644 --- a/services/gunDB/contact-api/utils/index.js +++ b/services/gunDB/contact-api/utils/index.js @@ -210,33 +210,21 @@ const tryAndWait = async (promGen, shouldRetry = () => false) => { * @param {string} pub * @returns {Promise} */ -const pubToEpub = async pub => { +const pubToEpub = pub => { try { - const epub = await tryAndWait(async gun => { - const _epub = await CommonUtils.makePromise(res => { - gun + return timeout10( + CommonUtils.makePromise(res => { + require('../../Mediator/index') + .getGun() .user(pub) .get('epub') - .once( - data => { + .on(data => { + if (typeof data === 'string') { res(data) - }, - { - wait: 1000 } - ) + }) }) - - if (typeof _epub !== 'string') { - throw new TypeError( - `Expected gun.user(pub).get(epub) to be an string. Instead got: ${typeof _epub}` - ) - } - - return _epub - }) - - return epub + ) } catch (err) { logger.error(err) throw new Error(`pubToEpub() -> ${err.message}`)