Home How to choose which function is called first in Node Js
Reply: 0

How to choose which function is called first in Node Js

user6205 Published in September 21, 2018, 8:04 am

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 .then keyword, but didn't manage to make it work.


share|improve this question
  • 2
    Call getGasPrice() function inside the .then() of getBlockNumber(), or take a look to async - await calls. – Iker Vázquez Feb 13 at 14:03
  • @IkerVázquez my god. Tried EVERYTHING except calling the getGasPrice() function insde the .then(). It works well, thank you. – iNukeLaPeste Feb 13 at 14:09

2 Answers 2

active oldest votes
up vote 1 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.362674 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO