Category : closures

function debounce(func, timeout = 300){ let timer; return (…args) => { clearTimeout(timer); timer = setTimeout(() => { func.apply(this, args); }, timeout); }; } function saveInput(){ console.log(‘Saving data’); } const processChange = debounce(() => saveInput()); Here’s a simple denounce function. What is the main reason for returning a function ? return (…args) => { clearTimeout(timer); timer ..

Read more

I have: const formats = [‘header’, ‘bold’, ‘italic’, ‘underline’, ‘list’, ‘prediction’, ‘accepted’, ‘mention’]; const modules = { mention: { allowedChars: /^[a-zA-Z0-9_]*$/, mentionDenotationChars: [‘@’], showDenotationChar: false, spaceAfterInsert: false, onSelect: (item, insertItem) => { clearDocumentState(0) const cursorPos = quill.getSelection() console.log({ cursorPos }) insertItem(item) }, source: () => { } }, toolbar: [ [‘bold’, ‘italic’, ‘underline’], [{ list: ..

Read more

I recently found this code online to reconstruct a promise from scratch: function MyPromise(configFunction) { let nextSuccessCallBack = undefined; let nextResolve = undefined; configFunction(function(message){ setTimeout(function(){ if(nextSuccessCallBack) { var result = nextSuccessCallBack(message); if(result && result.then) { result.then(nextResolve); } else { nextResolve && nextResolve(result); } } }) }); return { then: function(successCallback) { nextSuccessCallBack = successCallback; return ..

Read more

I have some experience with class components in React, but am trying to learn hooks and functional components better. I have the following code: import React, { useState } from "react"; import { Button } from "reactstrap"; import StyleRow from "./StyleRow"; export default function Controls(props) { const [styles, setStyles] = useState([]); function removeStyle(index) { let ..

Read more