From 2f28af29ddff2bccf20a7321ccd834f3d9a6ff5b Mon Sep 17 00:00:00 2001 From: Abdullah Alfaraj Date: Wed, 22 Feb 2023 16:36:44 +0300 Subject: [PATCH] add ability to await async callback functions --- thumbnail.js | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/thumbnail.js b/thumbnail.js index 063265e..3342fc8 100644 --- a/thumbnail.js +++ b/thumbnail.js @@ -1,25 +1,38 @@ class Thumbnail { static wrapImgInContainer(img, container_style_class) { - const container = document.createElement('div'); - container.className = container_style_class; - container.appendChild(img); - return container; + const container = document.createElement('div') + container.className = container_style_class + container.appendChild(img) + return container } - static addSPButtonToContainer(container, button_id, title, callbackFunction, param1) { - const elem = document.getElementById(button_id); - const clone = elem.cloneNode(true); + static addSPButtonToContainer( + container, + button_id, + title, + callbackFunction, + param1 + ) { + const elem = document.getElementById(button_id) + const clone = elem.cloneNode(true) const button = clone button.style.display = null button.removeAttribute('id') button.setAttribute('title', title) // Create button element - button.className = "thumbnail-image-button"; - button.addEventListener('click', () => callbackFunction(param1)) + button.className = 'thumbnail-image-button' + if (callbackFunction.constructor.name === 'AsyncFunction') { + button.addEventListener( + 'click', + async () => await callbackFunction(param1) + ) + } else { + button.addEventListener('click', () => callbackFunction(param1)) + } + container.appendChild(button) } - } module.exports = {