0

In this code client.createTransaction() function returns result1.txid. From that result1.txid i want to run while loop and inside that while loop I want to client.getTx() repeatably untill i get status == 1 or -1. Here i cant able to run the while loop. It shows nothing,

var coinPay = require('coinpayments');
var fs = require('fs');
var async = require("async");

var client = new coinPay({
  'key': 'XYZsdgdfgdf',
  'secret': 'XYZsdfsdfsd',
  'autoIpn': true
});

client.createTransaction({
    'currency1': 'LTCT',
    'currency2': 'LTCT',
    'amount': 1
  }, (err, result1) => {
    while (true) {
      client.getTx(result1.txn_id, (err, result) => {
        console.log(result);
        if (result.status == -1) {
          console.log("unsuccesful");
          return;
        } else if (result.status == 1) {
          console.log("succesful");
          return;
        } else
          console.log("checking");
      })
    }
  }
)
wowkin2
  • 5,895
  • 5
  • 23
  • 66
alt j
  • 11
  • 5
  • ok please share a link in comment. – alt j Aug 04 '18 at 07:18
  • instead of wasting mine and your time you could have ignored or answered my question as per code. we are learning developer not a phd guys who are being lazy. so please if u your planing to duplicate the question atleast mention your view that can be understandably with my question – alt j Aug 04 '18 at 09:01
  • Use async await – Helping hand Aug 04 '18 at 10:31
  • can you help me with an example? please i have tried async await – alt j Aug 04 '18 at 11:08
  • Did you read the duplicate? It explains why you can't use a `while()` loop like that in Javascript because you can't get any events while you're in the `while` loop, thus no async callbacks will ever get called. What did you not understand about the duplicate? Welcome to stack overflow, but the protocol here is that when we see a question that we've seen many answers to before, we are not supposed to repeat the same type of answer over and over - we are supposed to mark it a duplicate. That's how this place is supposed to work. If you don't understand something in the dup, you can comment. – jfriend00 Aug 04 '18 at 21:14
  • Other potentially useful references: [How the event loop and Ajax calls work](https://stackoverflow.com/questions/7575589/how-does-javascript-handle-ajax-responses-in-the-background/7575649#7575649), [Never ending while loop](https://stackoverflow.com/questions/38676709/javascript-what-is-wrong-with-this-while-loop-never-ending-loop/38676722#38676722), [Asynchronous operations inside a while loop](https://stackoverflow.com/questions/49790159/use-settimeout-inside-while-loop-in-node-js/49791567#49791567). – jfriend00 Aug 04 '18 at 21:17
  • More references: [While loop with async Ajax calls](https://stackoverflow.com/questions/19332049/while-loop-with-jquery-async-ajax-calls/19332078#19332078), [Looping waiting for async result](https://stackoverflow.com/questions/41494256/how-to-make-decisions-based-on-return-value-of-async-calls/41494768#41494768), [How to synchronize a sequence of promises](https://stackoverflow.com/questions/29880715/how-to-synchronize-a-sequence-of-promises/29906506#29906506). – jfriend00 Aug 04 '18 at 21:21
  • More references: [Javascript callback within while loop does not work](https://stackoverflow.com/questions/41175353/javascript-callback-within-a-while-loop-does-not-work), [Javascript while loop waits for increment instead of becoming infinite loop](https://stackoverflow.com/questions/44937588/javascript-while-loop-waits-for-increment-instead-of-becoming-an-infinite-loop) – jfriend00 Aug 04 '18 at 21:29

0 Answers0