aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/js/utils')
-rw-r--r--src/js/utils/elements.js2
-rw-r--r--src/js/utils/promise.js27
2 files changed, 28 insertions, 1 deletions
diff --git a/src/js/utils/elements.js b/src/js/utils/elements.js
index b88aad0c..43f46416 100644
--- a/src/js/utils/elements.js
+++ b/src/js/utils/elements.js
@@ -221,7 +221,7 @@ export function hasClass(element, className) {
// Element matches selector
export function matches(element, selector) {
- const prototype = { Element };
+ const prototype = Element.prototype;
function match() {
return Array.from(document.querySelectorAll(selector)).includes(this);
diff --git a/src/js/utils/promise.js b/src/js/utils/promise.js
new file mode 100644
index 00000000..42fcc2c3
--- /dev/null
+++ b/src/js/utils/promise.js
@@ -0,0 +1,27 @@
+/**
+ * Returns whether an object is `Promise`-like (i.e. has a `then` method).
+ *
+ * @param {Object} value
+ * An object that may or may not be `Promise`-like.
+ *
+ * @return {boolean}
+ * Whether or not the object is `Promise`-like.
+ */
+export function isPromise(value) {
+ return value !== undefined && value !== null && typeof value.then === 'function';
+}
+
+/**
+ * Silence a Promise-like object.
+ *
+ * This is useful for avoiding non-harmful, but potentially confusing "uncaught
+ * play promise" rejection error messages.
+ *
+ * @param {Object} value
+ * An object that may or may not be `Promise`-like.
+ */
+export function silencePromise(value) {
+ if (isPromise(value)) {
+ value.then(null, () => {});
+ }
+}