Feature Request "getVariables"
DazWilkin opened this issue · 5 comments
Happy New Year!
Using your library (again) this week, I think it would be useful to add a getVariables function:
lunchPlansWeek = runtimeConfig.getVariables('lunch-plans', ['monday', 'tuesday', ...]);
Perhaps Promise.all the individual calls in your library and only return successes?
Found this neat trick that effectively ignores rejects:
Promise.all(promises.map(p => p.catch(() => undefined)))
I may submit a PR for it.
Happy New Year! 🎉
That would be very convenient to have, I like it!
What would be the use case to silently ignore rejections though? I imagine, at least in the scenarios I've used it, that it would be problematic if some variables failed to load (API keys, etc).
Laziness :-(
It's unclear to me how to reconcile an array of Promises that contain resolves and rejects.
By ignoring rejects, the code is simpler... In my code currently, I have:
const getVariables = (runtimeConfig, config, variables) =>
Promise
.all(variables
.map(variable =>
runtimeConfig.getVariable(config, variable);
)
.map(p =>
p.catch(() => undefined)
)
);
Haha fair enough
I like the idea of keeping it simple but as a library maybe we should leave the error handling to the user.
A user could handle it with a catch
that is applied to getVariables
. Using the mechanics of Promise.all
; If all variables are loaded then
is called with an array of the values. If not catch
is called with the first rejection.
The API could look something like this:
runtimeConfig.getVariables('lunch-plans', ['monday', 'tuesday'])
.then(values => {
// this is executed if all values are loaded successfully
// values: ['valueOfMonday', 'valueOfTuesday']
})
.catch(reason => {
// this is called for the first variable that rejects
});
This does not handle the case where, after some variable rejects, we still want to continue with the other variables but that might be out of scope here unless we can find a general way of dealing with that.
Ah, you're good. Thank you!