Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert `web3` asynchronous calls into Promises.
module.exports = {
promisify: function (web3) {
// Pipes values from a Web3 callback.
var callbackToResolve = function (resolve, reject) {
return function (error, value) {
if (error) {
reject(error);
} else {
resolve(value);
}
};
};
// List synchronous functions masquerading as values.
var syncGetters = {
db: [],
eth: [ "accounts", "blockNumber", "coinbase", "gasPrice", "hashrate",
"mining", "protocolVersion", "syncing" ],
net: [ "listening", "peerCount" ],
personal: [ "listAccounts" ],
shh: [],
version: [ "ethereum", "network", "node", "whisper" ]
};
Object.keys(syncGetters).forEach(function(group) {
Object.keys(web3[group]).forEach(function (method) {
if (syncGetters[group].indexOf(method) > -1) {
// Skip
} else if (typeof web3[group][method] === "function") {
web3[group][method + "Promise"] = function () {
var args = arguments;
return new Promise(function (resolve, reject) {
args[args.length] = callbackToResolve(resolve, reject);
args.length++;
web3[group][method].apply(web3[group], args);
});
};
}
});
});
},
};
@xavierlepretre

This comment has been minimized.

Copy link
Owner Author

xavierlepretre commented Jan 17, 2017

You can use it like so:

web3.eth.getTransactionPromise(txHash)
    .then(function(tx) {
        console.log(tx);
    });
@xavierlepretre

This comment has been minimized.

Copy link
Owner Author

xavierlepretre commented Jan 20, 2017

You prepare it like so:

const PromisifyWeb3 = require("./promisifyWeb3.js");
PromisifyWeb3.promisify(web3);
@renexdev

This comment has been minimized.

Copy link

renexdev commented Aug 18, 2017

Hi @xavierlepretre, thanks for sharing your contributions... I'm quite newbie with js dev, and now dealing and learning all about this upgraded version of Web3 with promises and calls... I'm trying to test your gists, seem to be very useful to deal with all these new web3 features. I'm developing adding the sources to my HTML and developing code in a separate myscript.js.
I started with the following code (I have a private network running with some basic contracts)

     var web3 = new Web3(new Web3.providers.HttpProvider("http://myLocalHostIP:myRpcPort"));
     PromisifyWeb3.promisify(web3);

I get: ReferenceError: PromisifyWeb3 is not defined[Learn More]
How should I call properly your functions?
I would also like to understand what to replace with in txHash? in the implementation web3.eth.getTransactionPromise(txHash).. may be any eth command like eth.sendTransaction({from: eth.accounts[0], to: eth.accounts[1], value: web3.toWei(3,"ether")})..
Thanks for your guidance...

@JohnAllen

This comment has been minimized.

Copy link

JohnAllen commented Sep 29, 2017

@renexdev You have to import PromisifyWeb3 like @xavierlepretre mentioned in the post above yours:

const PromisifyWeb3 = require("./promisifyWeb3.js");
PromisifyWeb3.promisify(web3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.