0

i have not been able to figure out asynchronous flow in nodejs for the life of me. i am currently trying to get a response from a server. which is no problem. and use that response to validate whether to add the statement to an array if true. and to ignore if false. currently this is what im using for testing. and in the log i have one statement with a response of false and one with true. but both are using the same address.

  var test = Object.keys(addressAmounts);
  test.forEach(function(address) {
  daemon.cmd('validateaddress', [address], function (results) {
   var validWorkerAddress = results[0].response.isvalid;
  //if (validWorkerAddress != "false") {
            //console.log("\n\n\n\n Worker: " + w + " GETS COINS! " + coin + "\n\n\n");
  //} else {
            //console.log("\n\n\n\n Worker: " + w + " NO COINS BRA! " + coin + "\n\n\n");
  //}
  //console.log("Test for " + w + " is " + results[0].response.isvalid);
  if (validWorkerAddress != 'false') {
    console.log("Test for " + address + " was ok on " + coin +' isvalid:'+ validWorkerAddress + "\n\n\n");
  } else {
    console.log("Test for " + address + " was NOT ok\n\n\n");
  }
          });
  });
Infernoman
  • 33
  • 8
  • add `.bind(this,address);` at the end of `function(results){...}` – maioman May 07 '15 at 07:45
  • i have asked this question multiple time with no help whatsoever. i have tried to figure out how to use a function inside of a loop but have not been able to learn correctly. ive tried using async aswell as a ton of different ways trying to do this. in multiple instances not just this one. – Infernoman May 07 '15 at 07:53
  • actually @maioman adding the above to the function after the closing } returns cannot read property isvalid of undefined. and the console.log(test{address]) is still returning as double. – Infernoman May 07 '15 at 08:03
  • if i change this loop to `for (var address in test) { console.log(test[address]); }` everything works great and the address will echo. but right when i add the second function inside of this loop everything screws up. – Infernoman May 07 '15 at 08:07
  • Okay i edited the loop added a forEach method. the echo is working the daemon.cmd is working. currently my check for if (validWorkerAddress != 'false') { is returning all of the statements even if they are "false" is there something im doing wrong there? – Infernoman May 07 '15 at 08:21

0 Answers0