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,370 points
621 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();

To know more about Node JS, It's recommended to join Node JS Online Course today.

answered Oct 8, 2018 by Omkar
• 69,220 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 27, 2018 in Blockchain by Shashank
• 10,400 points
1,698 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,740 points
808 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,790 points
1,946 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,470 views
+1 vote
1 answer

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

yes all are over TCP/IP connections secured ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,460 points
1,460 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
• 69,220 points
1,139 views
+2 votes
3 answers
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP