Within a function, I need to call another in which I have used promises and return a series of data that I need to follow.
My problem is that I do not know if I'm doing it right or not, because does not throw an error but it returns an empty object. If I show it with console.log, it returns: Object Promise
The code is:
getData: function (results) {
return new Promise(function (resolve, reject) {
Common.getProvider(results).then(function(provider) {
provider.getData().then(function (providerData) {
var name = providerData.name;
var type = providerData.type;
var lang = providerData.language;
var data = [];
data.push(name);
data.push(type);
data.push(lang);
resolve(data);
},
function (error) {
reject(error);
console.log("Error 1", error);
}
);
},
function (error) {
console.log("Error 2", error);
reject(error);
})
.catch(
function (error) {
console.log("Error catch", error);
reject(error);
});
});
},
And in the next function I need to work with that data (data):
extractData: function(results) {
var resultsPDF = [];
var resultsImg = [];
for (var i = 0; i < results.length; i++) {
if (results[i].fileMimeType == "application/pdf") {
resultsPDF.push(results[i]);
}
else {
resultsImg.push(results[i]);
}
}
if (resultsPDF.length > 0) {
var datosQnecesito = Controller.getData(resultsPDF);
console.log("DATOS = "+ datosQnecesito); //Devuelve Object Promise
console.log(JSON.stringify(prov)); //Devuelve {}
//A partir de aquí necesitaré trabajar con esos datos que recibo
//...
}
}
How do I collect the data that returns the promise correctly so I can later work with them?
If there is an alternative way to do it, you can also help me