How do you make it asynchronous?



  • Annex NodeJS. There's a piece of the code:

    var isAccepted;
    offers.getOffer({tradeOfferId: offer.tradeofferid}, function (err, o) { 
       if (err || (o == null) || (typeof o.response.offer.trade_offer_state == "undefined")) {
          helper.msg('Cant load offer state. It can be accepted, but we cant check it. Returning');
          isAccepted = false;
          return;
       }
       if (o.response.offer.trade_offer_state == "3") {
       isAccepted = true;
       }
       else {
       isAccepted = false;
       }
       return;
    });
    

    if (!isAccepted) {
    io.emit('tradeoffer-msg', { steamid: offer.steamid_other, type: 6 });
    return;
    }

    The problem is, if (!isAccepted) done before we get the result from the server to offers.getOffer
    Of course I could put it.

     if (!isAccepted) {
    io.emit('tradeoffer-msg', { steamid: offer.steamid_other, type: 6 });
    return;
    }

    Total offers.getOfferBut then return will work for an unnamed function offers.getOfferNot "outside" like I do.
    Question: How do you get the annex to wait for an answer in this case?



  • You don't do asychronous return, it doesn't happen. Bring more code, because the problem is not in this piece. You need to use the callback function in any option instead of return.

    var isAccepted;
    offers.getOffer({tradeOfferId: offer.tradeofferid}, function (err, o) { 
       if (err || (o == null) || (typeof o.response.offer.trade_offer_state == "undefined")) {
          helper.msg('Cant load offer state. It can be accepted, but we cant check it. Returning');
          isAccepted = false;
          return;
       }
       if (o.response.offer.trade_offer_state == "3") {
       isAccepted = true;
       }
       else {
       isAccepted = false;
       }
       if (!isAccepted) {
          io.emit('tradeoffer-msg', { steamid: offer.steamid_other, type: 6 });
          callback()
       }
    });
    

    P.S. You can make it sound like it's synchronized, but these things write crab cancers.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2