2

I've a dispatcher in node js called dis.js. in this dispatcher i want to call a function in makepayment.js and wait for the result of submit transaction.

dis.js critical piece of code

const mk = require('./TCKT_MakePayment.js');
var stato = mk.makePayment("aboirio@clikoiljappj.it","signoresercente@esercente.com","1"); 
    // source addr,destaddr,amount

MAKEPAYMENT.JS

.then(function(sourceAccount) {

                    transaction = new StellarSdk.TransactionBuilder(sourceAccount)
                        .addOperation(StellarSdk.Operation.payment({
                            destination: des.publicKey(),
                            asset: assetTCKT,
                            amount: amount
                        }))
                        .addMemo(StellarSdk.Memo.text('Pagamento ad esercente'))
                        .build();
                    transaction.sign(sourceKeys); // firmo con account DISTRIBUTORE

                    return server.submitTransaction(transaction); //invio con account distributore che di fatto crea gli account
                })
                .then(function(result) {
                    console.log("Transazione effettuata a " + destaddr);
                    ****HOW CAN I PERFORM A RETURN HERE?****
                })
                .catch(function(err) {
                    ****HOW CAN I PERFORM A RETURN HERE?****
                });
  • 1
    To clarify, do you want to perform an action after the transaction has been successfully submitted to the network or after the transaction has been included in a ledger? – Rob May 21 '18 at 18:20
2

Look up how promises in JavaScript work -- here's a great resource for that: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises

Essentially, you chain "then" statements and the return value from each one is passed to the next one. The result of the last "then" or "catch" is what you can use in the promise you return.

So you're on the right track, but read up a bit more and things will click.

  return loadAccount(source).then(function(sourceAccount) {
    // snip ...
    transaction.sign(sourceKeys); // firmo con account DISTRIBUTORE

    return server.submitTransaction(transaction); 
   })
   .then(function(result) {
     console.log("Transazione effettuata a " + destaddr);
     return result; // this will allow the value to flow through to wherever you return it
   })
   .catch(function(err) {
      console.error(err);
      // you have a few options here:
      // throw err; // rethrow it so you can catch it later
      // return { error: true }; // "handle" the error here so it gets passed to the next then
   });

dis.js

var stato = mk.makePayment("aboirio@clikoiljappj.it","signoresercente@esercente.com","1"); // this is actually a Promise at this point
stato
  .then(function(result) {
     // this is the last returned result in the promise chain i created in makepayment -- in our example before it's just the result
  })
  .catch(function(err) {
   // if i rethrew it or didn't add a .catch block to makepayment then I can catch and handle it here
  })

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.