fix: promise resolving in reducer function

This commit is contained in:
Sérgio Salgado 2022-01-24 20:17:54 +00:00
parent fc5a13fcf3
commit 92efea080e

View file

@ -122,23 +122,25 @@ function getBlockchainSyncStatus (cryptoList) {
const installedButNotConfigured = []
const blockchainStatuses = _.reduce((acc, value) => {
const processStatus = common.es(`sudo supervisorctl status ${value.code} | awk '{ print $2 }'`).trim()
return acc.then(a => {
return wallet.checkBlockchainStatus(settings, value.cryptoCode)
.then(res => _.includes(value.cryptoCode, _.keys(installedCryptos) ? ({ ...acc, [value.cryptoCode]: res }) : ({ ...acc })))
.then(res => _.includes(value.cryptoCode, _.keys(installedCryptos)) ? Promise.resolve({ ...a, [value.cryptoCode]: res }) : Promise.resolve({ ...a }))
.catch(() => {
console.log('processStatus', processStatus)
if (processStatus === 'RUNNING') {
installedButNotConfigured.push(value.cryptoCode)
return { ...acc, [value.cryptoCode]: 'syncing' }
return Promise.resolve({ ...a, [value.cryptoCode]: 'syncing' })
}
return { ...acc }
return Promise.resolve({ ...a })
})
})
},
{},
Promise.resolve({}),
cryptoList
)
return { blockchainStatuses, installedButNotConfigured }
return Promise.all([blockchainStatuses, installedButNotConfigured])
})
.then(([blockchainStatuses, installedButNotConfigured]) => ({ blockchainStatuses, installedButNotConfigured }))
}
function run () {
@ -163,10 +165,14 @@ function run () {
return getBlockchainSyncStatus(cryptos)
.then(({ blockchainStatuses, installedButNotConfigured }) => {
if (!_.isEmpty(installedButNotConfigured)) {
return { message: `Detected ${_.join(' and ', installedButNotConfigured)} installed on this machine, but couldn't establish connection. ${_.size(installedButNotConfigured) === 1 ? `Is this plugin` : `Are these plugins`} configured via admin?`, isValid: false}
logger.warn(`Detected ${_.join(' and ', installedButNotConfigured)} installed on this machine, but couldn't establish connection. ${_.size(installedButNotConfigured) === 1 ? `Is this plugin` : `Are these plugins`} configured via admin?`)
}
const result = _.reduce((acc, value) => ({ ...acc, [value]: _.isNil(acc[value]) ? 1 : acc[value] + 1 }), {}, _.values(blockchainStatuses))
if (_.size(answers) + result.syncing > 2) {
return { message: `Installing these coins would pass the 2 parallel blockchain synchronization limit. Please try again with fewer coins or try again later.`, isValid: false }
}
if (result.syncing > 2) {
return { message: `There are currently more than 2 blockchains in their initial synchronization. Please try again later.`, isValid: false }
}
@ -183,12 +189,11 @@ function run () {
})
inquirer.prompt(questions)
.then(answers => Promise.all([validateAnswers(answers), answers]))
.then(answers => Promise.all([validateAnswers(answers.crypto), answers]))
.then(([res, answers]) => {
console.log('res', res)
if (res.isValid) {
return processCryptos(answers.crypto)
}
console.log(res.message)
logger.error(res.message)
})
}