I'm grabbing a value from somewhere and wanting to use it locally (using lodash):
const setting = _.get(this.settings, 'options');
this.settings.options
is set somewhere else, and depending on the environment, this.settings.options
may be undefined. In that case, I can do something like:
const set = (setting) => {
...
...
};
const getThing = () => {
setTimeout(() => {
const setting = _.get(this.settings, 'options');
return setting ? set(setting) : getThing();
}, 1000);
};
getThing();
This uses a setTimeout
function to wait 1 second for this.settings.options
to be set in that time, and if it's not yet set, the setTimeout
function calls itself to check one second later. After that, it continues to the set()
function with the acquired data. This seems to work every time, but it would be really nice if I could keep checking for this value until it is defined without a timer. I'm not sure if that's possible?
I've been trying to implement either a promise or use async / await to do this, but the examples I've seen also seem to use setTimeout
s, and the solution ends up seemingly more complicated. Among others, I've mainly been looking at this answer. Regardless, I'm having trouble doing this with async / await and will have to continue troubleshooting that.
Is there a way to wait for this value to be defined without using a setTimeout
or setInterval
?