I'm handling with a "Switch" diferent actions. In this case Im trying to add to the variable "data" just one product into a Json object. The function giveMeProd.. should return the value.
case "pvc":
if (option == 1) { //1 means I want a single prod.
data = giveMeTheProductPlease(sessionStorage.baseId);
console.log("result: " + data); ...
The giveMeTheProduct function:
function giveMeTheProductPlease(id) {
var aux = null;
$.getJSON("./data/results.json", function (data) {
var l = Object.keys(data).length;
for (var i = 0; i < l; i++) {
if (data[i].id == id) {
console.log(data[i]);
aux = data[i];
}
}
})
return aux;
}
I have been reading about the matter, it seems Im trying to run a asyncronous function and return it into an syncronous function. It could be solved with a "callback", but I need to recieve the "id" parameter and I dont know how to do it.
Any idea?