You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
654 B
JavaScript

'use strict';
const promiseTry = require("es6-promise-try");
function evaluateValue(value) {
if (typeof value === "function") {
return value();
} else {
return value;
}
}
function defaultValue(value, fallbackValue) {
value = evaluateValue(value);
if (value != null) {
return value;
} else {
return evaluateValue(fallbackValue);
}
}
defaultValue.async = function defaultAsyncValue(value, fallbackValue) {
return promiseTry(() => {
return evaluateValue(value);
}).then((resultValue) => {
if (resultValue != null) {
return resultValue;
} else {
return evaluateValue(fallbackValue);
}
})
}
module.exports = defaultValue;