Maintaining Context In Asynchronous JS: web3.eth.getBalance() as Example












0















I'm currently working with nodejs and web3, and running into problems with the asynchronous model, and perhaps the specific implementation of web3's calls. The code below will provide my specific problem, and I believe the answer will be a pattern that can be used elsewhere.



The core question: how do I get the balance using the getBalance request while retaining context of the account for which I queried?



Code simplified for brevity.



I have a list:



var ethAccounts = ['0xabc', '0xdef', '0x123'];


My code will iterate through the list, getting the balance of each and doing something with it. Simple, right? I can use getBalance(), prototyped as:



web3.eth.getBalance( account, blockNumber(optional), callback (optional)  )


The catch: getBalance is an asynchronous call that returns a Promise.



var balance = web3.eth.getBalance( ethAccounts[0] );


.. balance won't be what I need when it returns.



The official guidance is:



web3.eth.getBalance("0x407d73d8a49eeb85d32cf465507dd71d507100c1")
.then(console.log);
> "1000000000000"


I don't need the value at the console, so I build a function to catch the value.



web3.eth.getBalance(ethAccounts[0]).then( 
function( value ) {
var balance = value;

// In this scope, I *can't see* ethAccounts[0]

console.log(balance);
}
);


In my function block, I need both the account and the value.



Perhaps the callback can help:



 function testCallback( value1, value2 ) {
console.log(value1 + ", " + value2 );
}
web3.eth.getBalance(ethAccounts[0], undefined, testCallback );


Console output will be:



100000000000000000000, undefined


I have tried a number of scenarios: adding parameters to the callback, to the handler function, trying to cheat the promise with "await". So far I don't have a single pattern through which I can retain context.



Am I missing a simple Javascript of asynchronous pattern? How can I receive the balance in the same scope as I have the account in the request?



Thanks much!










share|improve this question























  • " // In this scope, I *can't see ethAccounts[0]"* Sure you can, at least in the example you provided. JavaScript has lexical scope. The only reason this wouldn't work is if ethAccounts or ethAccounts[0] gets modified before the callback is executed. Please provide a Minimal, Complete, and Verifiable example.

    – Felix Kling
    Nov 20 '18 at 21:56













  • What was the problem with await?

    – nikos fotiadis
    Nov 21 '18 at 7:01
















0















I'm currently working with nodejs and web3, and running into problems with the asynchronous model, and perhaps the specific implementation of web3's calls. The code below will provide my specific problem, and I believe the answer will be a pattern that can be used elsewhere.



The core question: how do I get the balance using the getBalance request while retaining context of the account for which I queried?



Code simplified for brevity.



I have a list:



var ethAccounts = ['0xabc', '0xdef', '0x123'];


My code will iterate through the list, getting the balance of each and doing something with it. Simple, right? I can use getBalance(), prototyped as:



web3.eth.getBalance( account, blockNumber(optional), callback (optional)  )


The catch: getBalance is an asynchronous call that returns a Promise.



var balance = web3.eth.getBalance( ethAccounts[0] );


.. balance won't be what I need when it returns.



The official guidance is:



web3.eth.getBalance("0x407d73d8a49eeb85d32cf465507dd71d507100c1")
.then(console.log);
> "1000000000000"


I don't need the value at the console, so I build a function to catch the value.



web3.eth.getBalance(ethAccounts[0]).then( 
function( value ) {
var balance = value;

// In this scope, I *can't see* ethAccounts[0]

console.log(balance);
}
);


In my function block, I need both the account and the value.



Perhaps the callback can help:



 function testCallback( value1, value2 ) {
console.log(value1 + ", " + value2 );
}
web3.eth.getBalance(ethAccounts[0], undefined, testCallback );


Console output will be:



100000000000000000000, undefined


I have tried a number of scenarios: adding parameters to the callback, to the handler function, trying to cheat the promise with "await". So far I don't have a single pattern through which I can retain context.



Am I missing a simple Javascript of asynchronous pattern? How can I receive the balance in the same scope as I have the account in the request?



Thanks much!










share|improve this question























  • " // In this scope, I *can't see ethAccounts[0]"* Sure you can, at least in the example you provided. JavaScript has lexical scope. The only reason this wouldn't work is if ethAccounts or ethAccounts[0] gets modified before the callback is executed. Please provide a Minimal, Complete, and Verifiable example.

    – Felix Kling
    Nov 20 '18 at 21:56













  • What was the problem with await?

    – nikos fotiadis
    Nov 21 '18 at 7:01














0












0








0








I'm currently working with nodejs and web3, and running into problems with the asynchronous model, and perhaps the specific implementation of web3's calls. The code below will provide my specific problem, and I believe the answer will be a pattern that can be used elsewhere.



The core question: how do I get the balance using the getBalance request while retaining context of the account for which I queried?



Code simplified for brevity.



I have a list:



var ethAccounts = ['0xabc', '0xdef', '0x123'];


My code will iterate through the list, getting the balance of each and doing something with it. Simple, right? I can use getBalance(), prototyped as:



web3.eth.getBalance( account, blockNumber(optional), callback (optional)  )


The catch: getBalance is an asynchronous call that returns a Promise.



var balance = web3.eth.getBalance( ethAccounts[0] );


.. balance won't be what I need when it returns.



The official guidance is:



web3.eth.getBalance("0x407d73d8a49eeb85d32cf465507dd71d507100c1")
.then(console.log);
> "1000000000000"


I don't need the value at the console, so I build a function to catch the value.



web3.eth.getBalance(ethAccounts[0]).then( 
function( value ) {
var balance = value;

// In this scope, I *can't see* ethAccounts[0]

console.log(balance);
}
);


In my function block, I need both the account and the value.



Perhaps the callback can help:



 function testCallback( value1, value2 ) {
console.log(value1 + ", " + value2 );
}
web3.eth.getBalance(ethAccounts[0], undefined, testCallback );


Console output will be:



100000000000000000000, undefined


I have tried a number of scenarios: adding parameters to the callback, to the handler function, trying to cheat the promise with "await". So far I don't have a single pattern through which I can retain context.



Am I missing a simple Javascript of asynchronous pattern? How can I receive the balance in the same scope as I have the account in the request?



Thanks much!










share|improve this question














I'm currently working with nodejs and web3, and running into problems with the asynchronous model, and perhaps the specific implementation of web3's calls. The code below will provide my specific problem, and I believe the answer will be a pattern that can be used elsewhere.



The core question: how do I get the balance using the getBalance request while retaining context of the account for which I queried?



Code simplified for brevity.



I have a list:



var ethAccounts = ['0xabc', '0xdef', '0x123'];


My code will iterate through the list, getting the balance of each and doing something with it. Simple, right? I can use getBalance(), prototyped as:



web3.eth.getBalance( account, blockNumber(optional), callback (optional)  )


The catch: getBalance is an asynchronous call that returns a Promise.



var balance = web3.eth.getBalance( ethAccounts[0] );


.. balance won't be what I need when it returns.



The official guidance is:



web3.eth.getBalance("0x407d73d8a49eeb85d32cf465507dd71d507100c1")
.then(console.log);
> "1000000000000"


I don't need the value at the console, so I build a function to catch the value.



web3.eth.getBalance(ethAccounts[0]).then( 
function( value ) {
var balance = value;

// In this scope, I *can't see* ethAccounts[0]

console.log(balance);
}
);


In my function block, I need both the account and the value.



Perhaps the callback can help:



 function testCallback( value1, value2 ) {
console.log(value1 + ", " + value2 );
}
web3.eth.getBalance(ethAccounts[0], undefined, testCallback );


Console output will be:



100000000000000000000, undefined


I have tried a number of scenarios: adding parameters to the callback, to the handler function, trying to cheat the promise with "await". So far I don't have a single pattern through which I can retain context.



Am I missing a simple Javascript of asynchronous pattern? How can I receive the balance in the same scope as I have the account in the request?



Thanks much!







javascript node.js asynchronous ethereum web3






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 21:53









Colin JaccinoColin Jaccino

1




1













  • " // In this scope, I *can't see ethAccounts[0]"* Sure you can, at least in the example you provided. JavaScript has lexical scope. The only reason this wouldn't work is if ethAccounts or ethAccounts[0] gets modified before the callback is executed. Please provide a Minimal, Complete, and Verifiable example.

    – Felix Kling
    Nov 20 '18 at 21:56













  • What was the problem with await?

    – nikos fotiadis
    Nov 21 '18 at 7:01



















  • " // In this scope, I *can't see ethAccounts[0]"* Sure you can, at least in the example you provided. JavaScript has lexical scope. The only reason this wouldn't work is if ethAccounts or ethAccounts[0] gets modified before the callback is executed. Please provide a Minimal, Complete, and Verifiable example.

    – Felix Kling
    Nov 20 '18 at 21:56













  • What was the problem with await?

    – nikos fotiadis
    Nov 21 '18 at 7:01

















" // In this scope, I *can't see ethAccounts[0]"* Sure you can, at least in the example you provided. JavaScript has lexical scope. The only reason this wouldn't work is if ethAccounts or ethAccounts[0] gets modified before the callback is executed. Please provide a Minimal, Complete, and Verifiable example.

– Felix Kling
Nov 20 '18 at 21:56







" // In this scope, I *can't see ethAccounts[0]"* Sure you can, at least in the example you provided. JavaScript has lexical scope. The only reason this wouldn't work is if ethAccounts or ethAccounts[0] gets modified before the callback is executed. Please provide a Minimal, Complete, and Verifiable example.

– Felix Kling
Nov 20 '18 at 21:56















What was the problem with await?

– nikos fotiadis
Nov 21 '18 at 7:01





What was the problem with await?

– nikos fotiadis
Nov 21 '18 at 7:01












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402153%2fmaintaining-context-in-asynchronous-js-web3-eth-getbalance-as-example%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402153%2fmaintaining-context-in-asynchronous-js-web3-eth-getbalance-as-example%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

'app-layout' is not a known element: how to share Component with different Modules

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

WPF add header to Image with URL pettitions [duplicate]