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
let timeoutID
const invoice = await Promise.race([
new Promise(res => {
const onMethod = new Promise(res => {
require('../Mediator')
.getGun()
.user(to)
@ -963,11 +962,27 @@ const sendPayment = async (to, amount, memo) => {
.get(orderID)
.on(inv => {
if (typeof inv === 'string') {
clearTimeout(timeoutID)
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) => {
setTimeout(() => {
rej(new Error(ErrorCode.ORDER_NOT_ANSWERED_IN_TIME))