P
P
patsanchique2020-04-13 15:29:13
Node.js
patsanchique, 2020-04-13 15:29:13

How to get data from node js async function?

I have a request to a third-party api that receives data from json in tradeItems
The request itself:

request({
    url: 'https://api.steampowered.com/IEconService/GetTradeOffers/v1/?key=MYAPIKEY&get_sent_offers=1&active_only=1&format=json',
json: true
}, (err, responser, body, undefined) => {
tradeItems = JSON.stringify(body.response['trade_offers_sent'][0].items_to_give);
});

I am trying to send this data to offer.addTheirItems(); to this part of the code:
client.on('webSession', function(sessionID, cookies) {
  manager.setCookies(cookies, function(err) {
    if (err) {
      console.log(err);
      process.exit(1); 
      return;
    }
    

let offer = manager.createOffer("https://steamcommunity.com/tradeoffer/new/?partner=123456789&token=B3VcmSg1");
      offer.addTheirItems();
      offer.setMessage("");
      offer.send(function(err, status) {
        if (err) {
          console.log(err);
          return;
        }

        if (status == 'pending') {
          
          console.log(`Offer #${offer.id} sent, but requires confirmation`);
          community.acceptConfirmationForObject("identitySecret", offer.id, function(err) {
            if (err) {
              console.log(err);
            } else {
              console.log("Offer confirmed");
            }
          });
        } else {
          console.log(`Offer #${offer.id} sent successfully`);
        }
      });
    
});
});

Please help with asynchronous function, I do not quite understand how to implement it

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Kiljko, 2020-04-13
@patsanchique

Look, your question is very unclear, but I will try to answer what I understand. See here an example of how async/await works:
You have an async function

async function testName() {
    // Code
  }

In order to execute the code in this function in order, you need to put the await keyword before the promise (a function that promises to do something).
async function testName() {
    await func1();
    await func2();
  }

You seem to be saying that the "testName" function should wait until the code in the "func1" function is executed, then in the "func2" function, after that continue executing the rest of the code.
And if you want to get data from the code that you indicated above first, then you need something like this construction:
async function testName() {
  const data = await request({
    url: 'https://api.steampowered.com/IEconService/GetTradeOffers/v1/?key=MYAPIKEY&get_sent_offers=1&active_only=1&format=json',
    json: true
  }, (err, responser, body, undefined) => {
    tradeItems = JSON.stringify(body.response['trade_offers_sent'][0].items_to_give);
  });
}

Now you will have in the data variable the response from the server to which you sent the request, in this case it is the steam marketplace. And then use data as you need.
However, you must understand that await is set for asynchronous methods, for example, a request that you send to the steam marketplace is an asynchronous request.
I strongly advise you to watch this video
. I hope it helped, I tried to explain it all as easily as possible.

A
Alexey Sundukov, 2020-04-13
@alekciy

Use promises.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question