Error using promises in JavaScript -


this question has answer here:

even though related ethereum, it's javascript question.

my goal have function deploy ethereum contract returns address once contract deployed (sidenote: i'm not interested in deploying mist or other options).

function deploy(contractname, accountowner, _gas) {     // contract code contracts     const input = fs.readfilesync('contracts/' + contractname + '.sol').tostring();     const output = solc.compile(input);     // trailing ':' needed otherwise crashes     const bytecode = output.contracts[':' + contractname].bytecode;     const abi = json.parse(output.contracts[':' + contractname].interface);     const contract = web3.eth.contract(abi);     const contractinstance = contract.new({         data: '0x' + bytecode,         from: accountowner,         gas: _gas     }, sendcontract(err, res));     contractinstance.then(console.log(contractinstance), console.log("failure")); }  function sendcontract(err, res) {     return new promise((resolve, reject) => {         if (err) {             console.log(err);             return reject(err);         } else {             console.log("transaction hash: " + res.transactionhash);             // if have address property, contract deployed             if (res.address) {                 console.log('contract address: ' + res.address);             resolve(res);             }         }     }) } 

this isn't working because returns referenceerror: err not defined. know it's related promise not sure how fix it, though have tried different things. please point me error?

i know there many questions here (1) have read them promises explanations (this one , this one, among others) , (2) stuck , appreciate help.

this isn't working because returns referenceerror: err not defined.

you define err argument function:

function sendcontract(err, res) 

as arguments, locally scoped variable function.

you attempt use variable here:

 }, sendcontract(err, res)); 

so attempt call sendcontract passing variable named err from outside function local variable of same name inside function.

since haven't defined err outside function, reference error.

you have same problem res don't see because err version triggers first.


i know it's related promise

it isn't.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -