How to Call web3.js Functions Synchronously – Node.js Guide

command-linenodejsweb3js

Installed web3.js 1.0.0-beta.26 on macOS
Running testrpc, Node.js based Ethereum client

After running node in my terminal and initiating the web3 object listening to port 8545:

> var Web3 = require("web3")
> web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")

I can only see my lists of accounts asynchronous like:

> web3.eth.getAccounts().then(console.log)

I want to do it synchronous like:

> web3.eth.accounts

Also for example web3.eth.getBalance() returns a promise. Where I before was able to run it synchronous.

I want to execute it like in this video: https://youtu.be/-5LhwoCcjp0?t=1m15s
Is this because I have installed the latest version or is there a possibility to run it synchronous.

Best Answer

No, currently web3 v1.0 only supports asynchronous operations.

You can install the previous version if you want synchronous behavior.

npm install web3@0.20

Related Topic