Skip to content

Instantly share code, notes, and snippets.

View reoxb's full-sized avatar

Juan Suarez reoxb

View GitHub Profile
@reoxb
reoxb / async_await.js
Created November 19, 2020 17:50
Async/await Madrid JS DayES
(async function () {
const a = await asyncFunction(2)
const b = await asyncFunction(a)
const c = await asyncFunction(b)
const d = await asyncFunction(c)
console.log(d);
})()
function asyncFunction(x, fn){
return new Promise(function (resolve, reject) {
@reoxb
reoxb / promise.js
Created November 19, 2020 20:57
How promises works
function asyncFunction(x) {
return new Promise(function (resolve, reject) {
if(typeof x === 'number' && !isNaN(x)){
resolve( x * x)
} else {
reject(new Error('It\'s not a number'))
}
})
}
@reoxb
reoxb / gist:6cf59bd1293f9f04aa669e4468de7eab
Created April 10, 2023 15:48
getKeyboardFocusableElements
/**
* Gets keyboard-focusable elements within a specified element
* @param {HTMLElement} [element=document] element
* @returns {Array}
*/
function getKeyboardFocusableElements(element = document) {
return [
...element.querySelectorAll(
'a[href], button, input, textarea, select, details,[tabindex]:not([tabindex="-1"])'
),
@reoxb
reoxb / App.js
Created September 15, 2024 03:29
React - Context API
import React, { useContext } from 'react';
import { AuthContext } from './AuthContext';
const UserProfile = () => {
const { user, logout } = useContext(AuthContext);
return (
<div>
<h2>Welcome, {user ? user.name : 'Guest'}</h2>
{user && <button onClick={logout}>Logout</button>}
@reoxb
reoxb / App.js
Last active September 15, 2024 04:52
React - Context API - Alternate
import "./App.css";
import React, { useContext } from 'react';
import Switch from "./Switch";
import { ThemeProvider, useTheme } from './ThemeContext'; // Import from ThemeContext.js
const Title = ({ children }) => {
const { theme } = useTheme();
return (
<h2
style={{