1

I have some Promises:

getJson = new Promise((resolve, reject) => {
        $.getJSON(abiJson)
        .done(abi => {
            resolve(abi);
        })
        .fail(() => {
            reject('Eror loading ABI '+name);
        });
    });
    initWeb3 = () => {
        return new Promise((resolve, reject) => {
            (typeof web3 !== 'undefined') ?
            resolve(new Web3(web3.currentProvider)):
            reject('No find wallet, or web3 is undefined!');
        });
    }
    initContract = () => {
        return new Promise((resolve) => {
            resolve(web3.eth.contract(abi).at(address));
        });
    }
    initWallet = () => {
        return new Promise((resolve, reject) => {
            (window.web3.currentProvider.isMetaMask) ? resolve('Metamask') :
            (window.web3.currentProvider.isTrust) ? resolve('Trust') :
            (window.web3.currentProvider.constructor.name === 'EthereumProvider') ? resolve('Mist') :
            (window.web3.currentProvider.constructor.name === 'Web3FrameProvider') ?  resolve('Parity') :
            reject('Not suported wallet');
        });
    }
    initAccount = () => {
        return new Promise((resolve, reject) => {
            (typeof web3.eth.accounts[0] !== 'undefined') ?
            resolve(web3.eth.accounts[0]) :
            reject('Please login to the wallet');
        });
    }

Help me to write promise chain correctly.
First of all I need to getJson and write resolve to some var
Then i need initWeb3() and write resolve to some var
Then initContract(), but I need get abi from getJson and send it to initContract() and write resolve to some var
Then initWallet() and write resolve to some var
And initAccount() and write resolve to some var
Help me to write promise chain correctly, and tell me please this is good code or 'shitcode'?

  • 1
    Apart from `getJSON`, I'm not sure why you need promises. Most of them appear to be promises wrapping synchronous operations/value returns. Why are you wrapping everything in promises? – Joseph Jan 25 '19 at 18:11
  • I am new in js, so can u show me how write this code correctly? – Vitali Malinovski Jan 25 '19 at 18:15
  • Avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it) for wrapping `getJSON`! – Bergi Jan 25 '19 at 18:30

1 Answers1

0

In your case you can just use then() as you don't seem you do blocking tasks except for the first ajax request at the beginning.

So you can do it straight forward like this:

getJson()
    .then(abi => {
      initWeb3();
      return abi;
     })
    .then(abi => {
      initContract();
      return abi;
     })
    .then(abi => {
      initWallet();
      return abi;
     })
    .then(initAccount) // you will find abi as the function argument
    .catch(e => console.error('something goes wrong', e));

Don't forget to use the catch() method at the end of the chin in order to get any exception that can raise in the middle of your calls.

That way is fragile, as if something bad appened in one of the call, all others will be not executed.

And even debugging can be a nightmare.

I think looking yo your code, you can do it better this way:

    getJson()
        .then(abi => { // here you have the result
        try {
            initWeb3();
            initContract();
            initWallet();
            initAccount();
        } catch(e) {
            // do something here
        }
    })
    .catch(e => console.error('something goes wrong', e));

As anything seems to be dependant of initWeb3 you should put all in a try catch.

In that way you have the promise failing for the ajax call only, and when suceded you just execute yous stuff.

If you have an error in this last part, you have e better way to handle this.

In this way you can use the abi returned by the ajax call.

Mario Santini
  • 2,905
  • 2
  • 20
  • 27