use try and wait routine

This commit is contained in:
Daniel Lugo 2020-02-29 19:16:23 -04:00
parent 38a8172431
commit 721a199e7d

View file

@ -954,8 +954,7 @@ const sendPayment = async (to, amount, memo) => {
// eslint-disable-next-line init-declarations // eslint-disable-next-line init-declarations
let timeoutID let timeoutID
const invoice = await Promise.race([ const onMethod = new Promise(res => {
new Promise(res => {
require('../Mediator') require('../Mediator')
.getGun() .getGun()
.user(to) .user(to)
@ -963,11 +962,27 @@ const sendPayment = async (to, amount, memo) => {
.get(orderID) .get(orderID)
.on(inv => { .on(inv => {
if (typeof inv === 'string') { if (typeof inv === 'string') {
clearTimeout(timeoutID)
res(inv) res(inv)
} }
}) })
})
const freshGunMethod = Utils.tryAndWait(
gun =>
gun
.user(to)
.get(Key.ORDER_TO_RESPONSE)
.get(orderID)
.then(),
v => typeof v === 'undefined'
)
const invoice = await Promise.race([
Promise.race([onMethod, freshGunMethod]).then(v => {
clearTimeout(timeoutID)
return v
}), }),
new Promise((_, rej) => { new Promise((_, rej) => {
setTimeout(() => { setTimeout(() => {
rej(new Error(ErrorCode.ORDER_NOT_ANSWERED_IN_TIME)) rej(new Error(ErrorCode.ORDER_NOT_ANSWERED_IN_TIME))