I'm programming a Particle Photon (like an Arduino). I'm trying to call a variable from it and then use that variable to add a .css class to a div.
The problem is that the variable keeps coming back as
undefined
on the initial call. However, if I call it again a couple of seconds later, it works fine.
This is the API call from Particle:
particle.getVariable({ deviceId: 'DEVICE_ID', name: 'temp', auth: token }).then(function(data) {
console.log('Device variable retrieved successfully:', data);
}, function(err) {
console.log('An error occurred while getting attrs:', err);
});
Here is my version of it:
function getVariable(varName) {
particle.getVariable({ deviceId: device_ID, name: varName, auth: accessToken }).then(function(data) {
return data.body.result;
}, function(err) {
console.log('An error occurred while getting variable:', err);
});
}
I'd like to be able to work with this like
$(".class").addClass(getVariable("var")
I'm pretty new to jQuery and Javascript, but I've been doing a ton of reading on callbacks and promises and getting nowhere. Any idea how to accomplish this?