javascript 在解析了包含嵌套承诺的承诺之后执行代码

6jjcrrmo  于 2023-01-16  发布在  Java
关注(0)|答案(2)|浏览(132)

我有下面的js代码结构;

Promise_1.then(function(){
  for(){
    Promise2.then(function(){
      ...
    })
  }
}).then(
  Promise_3.then(function(){
    for(){
      Promise4.then(function(){
        ...
      })
    }
  })
).then(
  function(){
    // SOME CODE
  }
)

我想执行一些代码后,上述承诺得到解决。但一些代码正在执行之前,上述承诺得到解决。我知道我可以封闭一些代码在setTimeout()这将解决问题所建议的其他答案的SO,但我认为这不是一个好主意。实际上,我的工作代码如下;

user_xp = 0
connections_blocks.methods.get_deposit_blocks(current_email).call({
    from: new_web3.eth.Contract.defaultAccount
}, function (err, result) {
    deposit_blocks = result
    console.log(deposit_blocks)
    deposit_blocks = deposit_blocks.split(",")
    deposit_blocks.splice(0, 1)
    for (i_ in deposit_blocks) {
        console.log(deposit_blocks[i_])
        user_xp + new_web3.eth.getBlock(deposit_blocks[i_]).then(function (deposit_block) {
            user_xp = user_xp + deposit_block.gasUsed
            console.log(user_xp)
        })
    }
}).then(
    connections_blocks.methods.get_send_blocks(current_email).call({
        from: new_web3.eth.Contract.defaultAccount
    }, function (err, result) {
        send_blocks = result
        console.log(send_blocks)
        send_blocks = send_blocks.split(",")
        send_blocks.splice(0, 1)
        for (i_ = 0; i_ < send_blocks.length; i_ = i_ + 2) {
            console.log(send_blocks[i_])
            user_xp + new_web3.eth.getBlock(send_blocks[i_]).then(function (send_block) {
                user_xp = user_xp + send_block.gasUsed
                console.log(user_xp)
            })
        }
    })).then(
    setTimeout(function () {
        console.log(user_xp)
        xp = document.getElementById('xp')
        xp.innerHTML = "XP:" + user_xp
    },1000)
)

在上面的代码中,我只是使用了setTimeout()来解决我的问题。但是我希望代码只有在上面的承诺被解决的时候才能自动执行。在JS中有什么简单的方法可以做到这一点吗?把承诺放在函数中,让它变得更复杂。

    • 更新**

我使用下面的web3函数从solidity的智能合约中获取数据,它实际返回一个带有promiseValue数据的承诺
myContract.方法. myMethod([参数). call(选项,[回调])

nfs0ujit

nfs0ujit1#

代码中的问题包括
1..then的参数必须是函数
1.如果需要等待循环中的Promises,则需要将它们添加到一个数组中,然后使用promise.all等待,直到它们都被解析
尝试对 actual 代码进行以下更改--我假设在没有节点样式回调的情况下,函数返回解析为result的promise,该promise将传递给节点样式回调(在此代码中已删除)
(if你等一会儿,我会把承诺链变平--我刚注意到它并不像它能做到的那样平

user_xp = 0
return connections_blocks.methods.get_deposit_blocks(current_email).call({
    from: new_web3.eth.Contract.defaultAccount
})
.then(function (result) {
    var promises = []
    deposit_blocks = result
    console.log(deposit_blocks)
    deposit_blocks = deposit_blocks.split(",")
    deposit_blocks.splice(0, 1)
    for (i_ in deposit_blocks) {
        console.log(deposit_blocks[i_])
        promises.push(
            new_web3.eth.getBlock(deposit_blocks[i_]).then(function (deposit_block) {
                user_xp = user_xp + deposit_block.gasUsed
                console.log(user_xp)
            })
        )
    }
    return Promise.all(promises)
})
.then(function () {
    return connections_blocks.methods.get_send_blocks(current_email).call({
        from: new_web3.eth.Contract.defaultAccount
    })
})
.then(function (result) {
    var promises=[]
    send_blocks = result
    console.log(send_blocks)
    send_blocks = send_blocks.split(",")
    send_blocks.splice(0, 1)
    for (i_ = 0; i_ < send_blocks.length; i_ = i_ + 2) {
        console.log(send_blocks[i_])
        promises.push(
            new_web3.eth.getBlock(send_blocks[i_]).then(function (send_block) {
                user_xp = user_xp + send_block.gasUsed
                console.log(user_xp)
            })
        )
    }
    return Promise.all(promises)
})
.then(function () {
    console.log(user_xp)
    xp = document.getElementById('xp')
    xp.innerHTML = "XP:" + user_xp
})

作为额外的“奖励”,我相信代码可以简化为

return connections_blocks.methods.get_deposit_blocks(current_email).call({
    from: new_web3.eth.Contract.defaultAccount
})
.then(result => Promise.all(result.split(",").slice(1).map(deposit_block => new_web3.eth.getBlock(deposit_block)
    .then(deposit_block => deposit_block.gasUsed)
})
.then(gasUsed1Array => connections_blocks.methods.get_send_blocks(current_email).call({
        from: new_web3.eth.Contract.defaultAccount
    })
    .then(result => Promise.all(result.split(",").slice(1).filter((v, i) => !(i%2)).map(send_blocks => new_web3.eth.getBlock(send_block)
        .then(send_block => send_block.gasUsed)
    )))
    .then(gasUsed2Array => [...gasUsed1Array, ...gasUsed2Array])
)
.then(results => {
    user_xp = results.reduce((a, b) => a + b);
    console.log(user_xp)
    xp = document.getElementById('xp')
    xp.innerHTML = "XP:" + user_xp
})
ktecyv1j

ktecyv1j2#

我假设Promise_1和Promise_3的解析将等待回调完成。如果是这种情况,这应该可以工作。

user_xp = 0

connections_blocks.methods.get_deposit_blocks(current_email).call({
    from: new_web3.eth.Contract.defaultAccount
}, function (err, result) {
    deposit_blocks = result
    console.log(deposit_blocks)
    deposit_blocks = deposit_blocks.split(",")
    deposit_blocks.splice(0, 1)
    // array to hold the promises
    const subPromises = []
    for (i_ in deposit_blocks) {
        console.log(deposit_blocks[i_])
        subPromises.push(new_web3.eth.getBlock(deposit_blocks[i_]))
    }
    // waiting for all promises to resolve
    Promise.all(subPromises).then(function (deposit_block) {
        user_xp = user_xp + deposit_block.gasUsed
        console.log(user_xp)
    })
})
.then(
    connections_blocks.methods.get_send_blocks(current_email).call({
        from: new_web3.eth.Contract.defaultAccount
    }, function (err, result) {
        send_blocks = result
        console.log(send_blocks)
        send_blocks = send_blocks.split(",")
        send_blocks.splice(0, 1)
        const subPromises = []
        for (i_ = 0; i_ < send_blocks.length; i_ = i_ + 2) {
            console.log(send_blocks[i_])
            subPromises.push(
                new_web3.eth.getBlock(send_blocks[i_])
            )
        }
        Promise.all(subPromises).then(function (send_block) {
            user_xp = user_xp + send_block.gasUsed
            console.log(user_xp)
        })
    })
)
.then(function () {
    console.log(user_xp)
    xp = document.getElementById('xp')
    xp.innerHTML = "XP:" + user_xp
})

相关问题