Skip to content

Instantly share code, notes, and snippets.

@xavierlepretre
Created January 8, 2017 17:36
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save xavierlepretre/90f0feafccc07b267e44a87050b95caa to your computer and use it in GitHub Desktop.
Save xavierlepretre/90f0feafccc07b267e44a87050b95caa to your computer and use it in GitHub Desktop.
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
Copy link
Author

You can use it like so:

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

@xavierlepretre
Copy link
Author

You prepare it like so:

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

@renexdev
Copy link

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
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