How to choose which function is called first in Node Js

0 votes

I am currently using an Ether package and NodeJS to learn a few things.

Here is my code:

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());
});

It basically gets the network from my argument, and then get blockNumber and gasPrice. So if run this, here is what I get:

Current block number: 5083149
Current gas price: 8000000000

Which is what I want. But sometimes, it gives the gas price BEFORE the block number, like that:

Current gas price: 8000000000
Current block number: 5083149

How can I edit my code so it always give the block number first? I tried to play with the .thenkeyword, but didn't manage to make it work.

Oct 8, 2018 in Blockchain by slayer
• 29,240 points
63 views

1 answer to this question.

0 votes

You can do something like:

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

Otherwise you're basically racing the two Promises and it may not be deterministic which one goes first.

Or, in an async function, you can do:

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();
answered Oct 8, 2018 by Omkar
• 68,180 points

Related Questions In Blockchain

0 votes
1 answer

How to create tokens in smart contracts from node.js?

Any modification to the Ethereum Blockchain will ...READ MORE

answered Jun 26, 2018 in Blockchain by Shashank
• 10,350 points
184 views
0 votes
1 answer

How to handle Overlapping of array objects in node js

Try this code, may be it works for ...READ MORE

answered Sep 18, 2018 in Blockchain by digger
• 26,600 points
42 views
0 votes
1 answer
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,810 points
186 views
0 votes
1 answer
0 votes
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured by TLS encryption in hashgraph architecture-hashgraph, ...READ MORE

answered Aug 6, 2018 in Blockchain by ariaholic
• 7,340 points
101 views
0 votes
1 answer

Docker: How to connect Node.js in container to Hyperledger Fabric?

You need to map ~/.composer/cards of your Docker host ...READ MORE

answered Oct 31, 2018 in Blockchain by Omkar
• 68,180 points
125 views
+1 vote
3 answers