Answer the question
In order to leave comments, you need to log in
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);
});
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`);
}
});
});
});
Answer the question
In order to leave comments, you need to log in
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
}
async function testName() {
await func1();
await func2();
}
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);
});
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question