Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何选择在Node Js中首先调用哪个函数_Node.js_Asynchronous_Ethereum - Fatal编程技术网

Node.js 如何选择在Node Js中首先调用哪个函数

Node.js 如何选择在Node Js中首先调用哪个函数,node.js,asynchronous,ethereum,Node.js,Asynchronous,Ethereum,我目前正在使用乙醚软件包和NodeJS学习一些东西 这是我的密码: var ethers = require('ethers'); var providers = require('ethers').providers; var infuraProvider = new providers.InfuraProvider(process.argv[3]); infuraProvider.getBlockNumber().then(function(blockNumber) { consol

我目前正在使用乙醚软件包和NodeJS学习一些东西

这是我的密码:

var ethers = require('ethers');
var providers = require('ethers').providers;

var infuraProvider = new providers.InfuraProvider(process.argv[3]);

infuraProvider.getBlockNumber().then(function(blockNumber) {
  console.log("Current block number: " + blockNumber);
});

infuraProvider.getGasPrice().then(function(gasPrice) {
  console.log("Current gas price: " + gasPrice.toString());
});
它基本上从我的论点中得到网络,然后得到blockNumber和gasPrice。 因此,如果运行此命令,我会得到以下结果:

Current block number: 5083149
Current gas price: 8000000000
这就是我想要的。但有时,它会在区块编号之前给出天然气价格,如下所示:

Current gas price: 8000000000
Current block number: 5083149
如何编辑代码,使其始终首先给出代码块编号?我试着玩
。然后
关键字,但没能成功


谢谢

您可以执行以下操作:

infuraProvider.getBlockNumber().then(function(blockNumber) {
    console.log("Current block number: " + blockNumber);
    infuraProvider.getGasPrice().then(function(gasPrice) {
        console.log("Current gas price: " + gasPrice.toString()); 
    });
});
否则,你基本上是在与这两个承诺赛跑,可能无法确定哪一个先做

或者,在异步函数中,您可以执行以下操作:

var printDetails = async function()
{
    let blockNumber = await infuraProvider.getBlockNumber();
    console.log("Current block number: " + blockNumber);
    let gasPrice = await infuraProvider.getGasPrice();
    console.log("Current gas price: " + gasPrice.toString());
}

printDetails();

您可以执行以下操作:

infuraProvider.getBlockNumber().then(function(blockNumber) {
    console.log("Current block number: " + blockNumber);
    infuraProvider.getGasPrice().then(function(gasPrice) {
        console.log("Current gas price: " + gasPrice.toString()); 
    });
});
否则,你基本上是在与这两个承诺赛跑,可能无法确定哪一个先做

或者,在异步函数中,您可以执行以下操作:

var printDetails = async function()
{
    let blockNumber = await infuraProvider.getBlockNumber();
    console.log("Current block number: " + blockNumber);
    let gasPrice = await infuraProvider.getGasPrice();
    console.log("Current gas price: " + gasPrice.toString());
}

printDetails();
试试这个

infuraProvider.getBlockNumber().then(function(blockNumber) {
  console.log("Current block number: " + blockNumber);
  return infuraProvider.getGasPrice();
}).then(function(gasPrice) {
  console.log("Current gas price: " + gasPrice.toString());
});
试试这个

infuraProvider.getBlockNumber().then(function(blockNumber) {
  console.log("Current block number: " + blockNumber);
  return infuraProvider.getGasPrice();
}).then(function(gasPrice) {
  console.log("Current gas price: " + gasPrice.toString());
});

在getBlockNumber()的.then()中调用getGasPrice()函数,或者查看异步等待调用。@IkerVázquez我的天哪。除了调用.then()中的getGasPrice()函数外,尝试了所有操作。它工作得很好,谢谢。在getBlockNumber()的.then()中调用getGasPrice()函数,或者看看异步等待调用。@IkerVázquez我的天哪。除了调用.then()中的getGasPrice()函数外,尝试了所有操作。很好,谢谢。非常感谢您的详细回复。就像您在第一个代码中所做的那样,只需将我的getGasPrice函数放在第一个函数中。但是,第一个示例和异步函数示例之间有什么区别?哪一个最好用?应该可以。连锁承诺是一种非常常见的模式,如果调用10个函数,您可能希望代码的结构稍微不同。你不想窝得太深。非常感谢你的详细回复。就像您在第一个代码中所做的那样,只需将我的getGasPrice函数放在第一个函数中。但是,第一个示例和异步函数示例之间有什么区别?哪一个最好用?应该可以。连锁承诺是一种非常常见的模式,如果调用10个函数,您可能希望代码的结构稍微不同。你不会想窝10深的。谢谢你的帮助,非常感谢。这种方式与将getGasPrice函数放入getBlockNumber()的.then()中有什么区别?就像Terry Lennox所做的那样。使用这种方法的优点是避免函数嵌套,现在你只有2个函数,所以没有问题,但如果函数更多,就会产生问题。好的,谢谢你的解释。多亏了你们两位,我才明白这一切。再次感谢!谢谢你的帮助,非常感谢。这种方式与将getGasPrice函数放入getBlockNumber()的.then()中有什么区别?就像Terry Lennox所做的那样。使用这种方法的优点是避免函数嵌套,现在你只有2个函数,所以没有问题,但如果函数更多,就会产生问题。好的,谢谢你的解释。多亏了你们两位,我才明白这一切。再次感谢!