回调函数外的响应未定义

时间:2019-09-13 04:01:30

标签: javascript node.js api eos

该请求正在运行,并输出blockInfo,但该范围之外的所有内容均未定义。有人可以帮我将正文保存到可以在其他地方使用的变量中。

const getInfo = () => {
    let request = require("request");

    let options = {
        method: 'GET',
        url: 'https://api.eosnewyork.io/v1/chain/get_info',
        headers: { accept: 'application/json' }
    };
    let blockInfo;

    request(options, function (error, response, body) {
        if (error) throw new Error(error);
        blockInfo = JSON.parse(body);
        console.log(blockInfo)
        return blockInfo
    });
    console.log(blockInfo)
    return blockInfo
}
const blockNum = getInfo()

0 个答案:

没有答案