I have an Alexa skill that takes the device's address and uses that information to find a nearby facility. According to this solution here, this is due to Javascript's asynchronicity.
The proposed solution in the above-linked question is to pull the http call into its own function with a callback. I have done that (see code below) but the http request is still not returning.
I have also taken a look at the solution here as proposed by @jfriend00. My callbacks are structured the same way. I still don't understand why my callbacks aren't returning before the rest of the code runs.
getKaiserBuildingHandler()
const getKaiserBuildingHandler = function() {
console.info("Starting getKaiserBuildingHandler()");
...
switch(addressResponse.statusCode) {
case 200:
console.log("Address successfully retrieved, now responding to user.");
...
const nearestKPFacility = getKaiserBuildingHelper(buildingType, address);
...
break;
...
}
console.info("Ending getKaiserBuildingHandler()");
});
...
};
getKaiserBuildingHelper()
const getKaiserBuildingHelper = function(buildingType, address) {
console.info("Starting getKaiserBuildingHelper()");
// var facilityOutput = Messages.ERROR;
var facilityOutput = "Inside building helper function, initial value.";
if (buildingType == BLDG_TYPE.PHARMACY ||
buildingType == BLDG_TYPE.CLINIC ||
buildingType == BLDG_TYPE.HOSPITAL) {
...
facilityOutput = "Before get JSON call.";
getJSON(buildingType, function(err, data) {
if (data != "ERROR") {
console.info("Received data from callback: ", data);
facilityOutput = "The closest Kaiser Permanente " + buildingType + " to your location is located at " + data + ".";
} else {
console.error("Error with data received from callback. ", err);
facilityOutput = "Entered JSON call, returned ERROR.";
}
});
}
...
return facilityOutput;
console.info("Ending getKaiserBuildingHelper()");
}
getJSON()
const getJSON = function(building, callback) {
console.info("Starting getJSON()");
Axios
.get(getFacilitySearchEndpoint(building))
.then(function(response) {
callback(null, response);
})
.catch(function(error) {
callback(error, "ERROR");
});
console.info("Ending getJSON()");
}
getFacilitySearchEndpoint() [wikipedia api just as placeholders]
const getFacilitySearchEndpoint = function(building) {
console.info("Starting getFacilitySearchEndpoint()");
switch (building) {
case BLDG_TYPE.HOSPITAL:
console.info("Ending getFacilitySearchEndpoint() with " + building);
return "https://en.wikipedia.org/w/api.php?action=query&format=json&list=search&utf8=1&srsearch=Albert+Einstein";
break;
case BLDG_TYPE.PHARMACY:
console.info("Ending getFacilitySearchEndpoint() with " + building);
return "https://en.wikipedia.org/w/api.php?action=query&format=json&list=search&utf8=1&srsearch=Harry+Potter";
break;
case BLDG_TYPE.CLINIC:
console.info("Ending getFacilitySearchEndpoint() with " + building);
return "https://en.wikipedia.org/w/api.php?action=query&format=json&list=search&utf8=1&srsearch=Tony+Stark";
break;
default:
console.info("Ending getFacilitySearchEndpoint() with default");
break;
}
console.info("Ending getFacilitySearchEndpoint()");
}