refactor: use pRetry to prevent recursive call
This commit is contained in:
parent
d1bf6f5f3c
commit
e326f4c0e5
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),
|
||||
|
|
|
|||
26
package-lock.json
generated
26
package-lock.json
generated
|
|
@ -1577,6 +1577,11 @@
|
|||
"resolved": "https://registry.npmjs.org/@types/range-parser/-/range-parser-1.2.3.tgz",
|
||||
"integrity": "sha512-ewFXqrQHlFsgc09MK5jP5iR7vumV/BYayNC6PgJO2LPe8vrnNFyjQjSppfEngITi0qvfKtzFvgKymGheFM9UOA=="
|
||||
},
|
||||
"@types/retry": {
|
||||
"version": "0.12.0",
|
||||
"resolved": "https://registry.npmjs.org/@types/retry/-/retry-0.12.0.tgz",
|
||||
"integrity": "sha512-wWKOClTTiizcZhXnPY4wikVAwmdYHp8q6DmC+EJUzAMsycb7HB32Kh9RN4+0gExjmPmZSAQjgURXIGATPegAvA=="
|
||||
},
|
||||
"@types/secp256k1": {
|
||||
"version": "4.0.1",
|
||||
"resolved": "https://registry.npmjs.org/@types/secp256k1/-/secp256k1-4.0.1.tgz",
|
||||
|
|
@ -7075,6 +7080,16 @@
|
|||
"timed-out": "^4.0.0",
|
||||
"url-parse-lax": "^1.0.0",
|
||||
"url-to-options": "^1.0.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"p-timeout": {
|
||||
"version": "1.2.1",
|
||||
"resolved": "https://registry.npmjs.org/p-timeout/-/p-timeout-1.2.1.tgz",
|
||||
"integrity": "sha1-XrOzU7f86Z8QGhA4iAuwVOu+o4Y=",
|
||||
"requires": {
|
||||
"p-finally": "^1.0.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"graceful-fs": {
|
||||
|
|
@ -11478,12 +11493,13 @@
|
|||
"resolved": "https://registry.npmjs.org/p-reduce/-/p-reduce-1.0.0.tgz",
|
||||
"integrity": "sha1-GMKw3ZNqRpClKfgjH1ig/bakffo="
|
||||
},
|
||||
"p-timeout": {
|
||||
"version": "1.2.1",
|
||||
"resolved": "https://registry.npmjs.org/p-timeout/-/p-timeout-1.2.1.tgz",
|
||||
"integrity": "sha1-XrOzU7f86Z8QGhA4iAuwVOu+o4Y=",
|
||||
"p-retry": {
|
||||
"version": "4.4.0",
|
||||
"resolved": "https://registry.npmjs.org/p-retry/-/p-retry-4.4.0.tgz",
|
||||
"integrity": "sha512-gVB/tBsG+3AHI1SyDHRrX6n9ZL0Bcbifps9W9/Bgu3Oyu4/OrAh8SvDzDsvpP0oxfCt3oWNT+0fQ9LyUGwBTLg==",
|
||||
"requires": {
|
||||
"p-finally": "^1.0.0"
|
||||
"@types/retry": "^0.12.0",
|
||||
"retry": "^0.12.0"
|
||||
}
|
||||
},
|
||||
"p-try": {
|
||||
|
|
|
|||
|
|
@ -59,6 +59,7 @@
|
|||
"nocache": "^2.1.0",
|
||||
"numeral": "^2.0.3",
|
||||
"p-each-series": "^1.0.0",
|
||||
"p-retry": "^4.4.0",
|
||||
"pg-native": "^3.0.0",
|
||||
"pg-promise": "^7.4.1",
|
||||
"pify": "^3.0.0",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue