I got a problem, I need to await a request to be finished in my Node.js code, I only get returned undefined or Promise pending and my code doesnt await for my array to be filled. Thanks alot
function request(){
var arrTours = [];
var p1 = new Promise(function (resolve) {
request(optionsauth, function (error, response) {
if (error) throw new Error(error);
var JSONResponseCode = JSON.parse(response.body)
var accesstoken = JSONResponseCode.access_token
getData(accesstoken)
});
function getData(accesstoken){
var options = {
'method': 'GET',
'url': 'https://google.com',
'headers': {
'Authorization': 'Bearer ' + accesstoken
},
formData: {
}
};
request(options, function (error, response) {
if (error) throw new Error(error);
const xml = response.body;
var TourLength = 5;
for (var i=0; i<TourLength; i++)
parser.parseString(xml, function (err, result) {
const Tour = {
Day: result[0]
};
arrTours.push(Tour)
})
resolve(arrTours)
});
}
})
}
async function f1() {
var x = await request();
return x
}
f1();