refactor: use pRetry to prevent recursive call
This commit is contained in:
parent
74d3691812
commit
7d07964e7e
3 changed files with 42 additions and 30 deletions
|
|
@ -1,4 +1,5 @@
|
|||
const _ = require('lodash/fp')
|
||||
const pRetry = require('p-retry')
|
||||
const jsonRpc = require('../../common/json-rpc')
|
||||
|
||||
const coinUtils = require('../../../coin-utils')
|
||||
|
|
@ -54,34 +55,28 @@ function sendCoins (account, address, cryptoAtoms, cryptoCode) {
|
|||
const coins = cryptoAtoms.shift(-unitScale).toFixed(8)
|
||||
const checkSendStatus = function (opid) {
|
||||
return new Promise((resolve, reject) => {
|
||||
function checker(timeout) {
|
||||
setTimeout(() => {
|
||||
fetch('z_getoperationstatus', [[opid]])
|
||||
.then(res => {
|
||||
const status = _.get('status', res[0])
|
||||
switch(status) {
|
||||
case 'success':
|
||||
resolve(res[0])
|
||||
break
|
||||
case 'failed':
|
||||
reject(res[0].error)
|
||||
break
|
||||
case 'executing':
|
||||
if (timeout > 0) {
|
||||
checker(timeout - 500)
|
||||
} else {
|
||||
reject(res)
|
||||
}
|
||||
}
|
||||
})
|
||||
}, 500)
|
||||
}
|
||||
checker(10000)
|
||||
fetch('z_getoperationstatus', [[opid]])
|
||||
.then(res => {
|
||||
const status = _.get('status', res[0])
|
||||
switch (status) {
|
||||
case 'success':
|
||||
resolve(res[0])
|
||||
break
|
||||
case 'failed':
|
||||
throw new pRetry.AbortError(res[0].error)
|
||||
case 'executing':
|
||||
reject(res[0].error)
|
||||
break
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
const checker = async () => {
|
||||
return pRetry(checkSendStatus, { retries: 20, minTimeout: 300, factor: 1.05 })
|
||||
}
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => fetch('z_sendmany', ["ANY_TADDR", [{address, amount: coins}]]))
|
||||
.then(checkSendStatus)
|
||||
.then(checker)
|
||||
.then((res) => {
|
||||
return {
|
||||
fee: _.get('params.fee', res),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue