Category: Javascript

Convert Callback to Promise in Publish Subscribe Model

Query asked by user Let say we have a service called A and it has the function subscribe(callback). The subscription from the service is an open connection that can receive data at any time, and the data can be accessed through the callback. Can we convert this callback to promise? if so, how? Sample A.subscribe((error, […]

Display different text values in an array with a button

Query asked by user Currently all values in the array are displayed at once when clicking on one of the two buttons. My goal is that the first value is displayed by default and with next/previous the next/previous array entry is displayed. How can I realize realize this simply? var array = [“text1”, “text2”, “text3”]; […]

Create multiple elements

Query asked by user JavaScript var textBlock = document.createElement(‘div’); textBlock.setAttribute(‘class’, ‘finalBlock’); var addFinalBlock = document.getElementsByClassName(‘block’)[0]; addFinalBlock.appendChild(textBlock); textBlock.innerHTML = “Make this block a text block”; // ——- var textBlock2 = document.createElement(‘div’); textBlock2.setAttribute(‘class’, ‘finalBlock’); var addFinalBlock2 = document.getElementsByClassName(‘block’)[0]; addFinalBlock2.appendChild(textBlock2); textBlock2.innerHTML = “Make this block a text block2”; // ——- var textBlock3 = document.createElement(‘div’); textBlock3.setAttribute(‘class’, ‘finalBlock’); var addFinalBlock3 […]

POST base64 png in XMLHTTP Request

Query asked by user For some reason this information is impossible to find a straight forward answer for, I have been trying to solve this for over a week. So please give a useful answer to it only! I am saving canvas.toDataUrl image into the server with the following steps: JAVASCRIPT function captureCanvas(){ html2canvas(displayScreen).then(canvas =>{ […]

Intents keep giving me a error even though the intents code is a C&P from a working bot

Query asked by user I have tried this which is a copy and paste from another one of my bots const client = new Discord.Client({ intents: [ Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MESSAGE_REACTIONS, Intents.FLAGS.GUILD_PRESENCES, ], }) Error: ReferenceError: Intents is not defined Answer we found from sources Simply, you should define the Intents by doing this: const { […]

Spying On/Mocking Import of an Import

Query asked by user I’m writing unit tests using vitest on a VueJS application. As part of our application, we have a collection of API wrapper services, e.g. users.js which wraps our relevant API calls to retrieve user information: import client from ‘./client’ const getUsers = () => { return client.get(…) } export default { […]

Get referer URL from external redirect to my application

Query asked by user Hi I’m new in React so will appreciate any help: www.google.com/home my user is on this Then gets redirected to my react app www.app.com How do I get google.com or /home? Perhaps: {pathname} = useLocations(); pathname.match(google.com) Thank you! Answer we found from sources The only way I am aware of is […]

Casting a number to a string in TypeScript

Query asked by user Which is the the best way (if there is one) to cast from number to string in Typescript? var page_number:number = 3; window.location.hash = page_number; In this case the compiler throws the error: Type ‘number’ is not assignable to type ‘string’ Because location.hash is a string. window.location.hash = “”+page_number; //casting using […]