luci-base: luci.js: add resolveDefault() helper
authorJo-Philipp Wich <jo@mein.io>
Wed, 2 Oct 2019 17:39:23 +0000 (19:39 +0200)
committerJo-Philipp Wich <jo@mein.io>
Mon, 7 Oct 2019 09:48:33 +0000 (11:48 +0200)
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
modules/luci-base/htdocs/luci-static/resources/luci.js

index cad7208532ef4f8367e0637e3191cfdca79e989f..3f4707d4e576d4875540cc4a6492b525ea95854a 100644 (file)
                        return s.split(/\s+/);
                },
 
+               /**
+                * Returns a promise resolving with either the given value or or with
+                * the given default in case the input value is a rejecting promise.
+                *
+                * @instance
+                * @memberof LuCI
+                *
+                * @param {*} value
+                * The value to resolve the promise with.
+                *
+                * @param {*} defvalue
+                * The default value to resolve the promise with in case the given
+                * input value is a rejecting promise.
+                *
+                * @returns {Promise<*>}
+                * Returns a new promise resolving either to the given input value or
+                * to the given default value on error.
+                */
+               resolveDefault: function(value, defvalue) {
+                       return Promise.resolve(value).catch(function() { return defvalue });
+               },
 
                /**
                 * The request callback function is invoked whenever an HTTP