【问题标题】:Function Parameters with Node.js PromiseNode.js Promise 的函数参数
【发布时间】:2018-11-02 10:09:28
【问题描述】:

我正在使用blockexplorer 区块链API,我想根据特定的哈希获取区块数据(这个哈希应该取自另一个函数)。

我是 Promise 新手,谁能帮我获取区块数据?

这是我的代码:

const be = require('blockexplorer');

be.block(be.blockIndex(0))
.then((result) => {
 console.log(result)
})
.catch((err) => {
throw err
})

另外,我尝试了另一种使用嵌套 Promise 的方法,但没有奏效。

【问题讨论】:

    标签: javascript node.js promise blockchain


    【解决方案1】:

    您可以让promise.then 返回一个承诺并进一步链接它。把代码修改成这样。

    be.blockIndex(0)
    .then((result) => be.block(result))
    .then((result) => {
       console.log(result)
    })
    .catch((err) => {
       console.log("Error Occurred: ", err); // Don't throw the error instead handle it
    });
    

    【讨论】:

    • 它不起作用,我收到了这个错误 [Unhandled reject StatusCodeError: 404 - "Not found"]
    • 这是因为您正在捕获错误然后将其抛出。始终捕获错误并处理它
    • 我已经修改了处理错误的答案请看一下
    猜你喜欢
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-28
    相关资源
    最近更新 更多