-2

I have this try statement and I wanted to update it with more if's and make it to try 2 more times before finally declining the offer. I did not succeed. I have no idea how to do them correctly. Does anyone know how to fix my statement after?

STATEMENT BEFORE (WORKS)

try {
    offers.acceptOffer({tradeOfferId: offer.tradeofferid}, function(err, log) {
        if (err) { 
            helper.log('Error accepting trade offer ' + offer.tradeofferid, 891, err);
            offers.declineOffer({tradeOfferId: offer.tradeofferid}, function() {
                currentGameOffers.splice(currentGameOffers.indexOf(offer.tradeofferid), 1);
            }); 
            return;
        }

STATEMENT AFTER (DON'T WORK)

try {
    offers.acceptOffer({tradeOfferId: offer.tradeofferid}, function(err, log) {
        if (err) { 
            helper.log('Error accepting trade offer: 1st try' + offer.tradeofferid, 891, err);
            offers.acceptOffer({tradeOfferId: offer.tradeofferid}, function(err, log) {
                if (err) { 
                    helper.log('Error accepting trade offer: 2nd try ' + offer.tradeofferid, 891, err);
                    offers.acceptOffer({tradeOfferId: offer.tradeofferid}, function(err, log) {
                        if (err) { 
                            helper.log('Error accepting trade offer: 3rd try ' + offer.tradeofferid, 891, err);
                            offers.acceptOffer({tradeOfferId: offer.tradeofferid}, function(err, log) {
                                if (err) { 
                                    helper.log('Offer declined: failed 3 times ' + offer.tradeofferid, 891, err);
                                    offers.declineOffer({tradeOfferId: offer.tradeofferid}, function() {
                                        currentGameOffers.splice(currentGameOffers.indexOf(offer.tradeofferid), 1);
                                    });
                                    return;
        }
Cœur
  • 37,241
  • 25
  • 195
  • 267
Petras
  • 19
  • 5
  • 1
    Use `promise` or `async`. This is callback hell! https://www.npmjs.com/package/async – Niezborala Dec 01 '16 at 16:04
  • 1
    what part of your code "does not work"? what are you expecting to see? what actually happens? are there any exceptions or errors in your console? – Dan O Dec 01 '16 at 16:06
  • could you guys explain why is this wrong? – Petras Dec 01 '16 at 16:08
  • @DanO well theres basically first try to accept offer "offers.acceptOffer' and if theres error it logs and declines. I want it to try 2 more times. – Petras Dec 01 '16 at 16:10
  • @Petras - Are you sure all the `{` have matching `}` and I think there is even some missing `)` but it's **extremely** hard to tell the way this is written. – Nope Dec 01 '16 at 16:10

1 Answers1

0

That code looks hard to read and maintain. It might work, but like you said, adding another few checks is a nightmare.

Take a look at the async library, namely the retry function

Now you code would look something like this: (code untested)

async.retry(3, function( retryCb, prevResult){ offers.acceptOffer({tradeOfferId: offer.tradeofferid}, retryCb); }, function(err, result) { if( err){ //decline offer } // do something with the result });

Now it's easy to add more checks, change the delay, etc. Thanks for helping with control flow, async! : )

clay
  • 5,917
  • 2
  • 23
  • 21