Skip to content

Instantly share code, notes, and snippets.

View mattiaerre's full-sized avatar
`yarn prettier:fix`

Mattia Richetto mattiaerre

`yarn prettier:fix`
View GitHub Profile
async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index += 1) {
// eslint-disable-next-line no-await-in-loop
await callback(array[index], index, array);
}
}
// credit to https://codeburst.io/javascript-async-await-with-foreach-b6ba62bbf404
@mattiaerre
mattiaerre / makeMenusClient.js
Created December 9, 2018 00:27
How to mock a module that exports a factory function with Jest
import fetch from 'node-fetch';
function makeMenusClient(context) {
return {
getMenus: async rid =>
fetch(`${context.menusBaseUrl}/${rid}`)
.then(response => response.json())
.then(json => json)
};
}
@mattiaerre
mattiaerre / README.md
Last active June 13, 2021 10:33
req, res and chaining

How to test it in an Express app

app.get('/playground', handler);

200

http://localhost:9000/playground

@mattiaerre
mattiaerre / useLocalStorage.js
Created April 29, 2020 02:12
React hook useReducerWithLocalStorage
import { useState } from 'react';
// credit: https://usehooks.com/useLocalStorage/
function useLocalStorage(key, initialValue) {
const [storedValue, setStoredValue] = useState(() => {
try {
const item = window.localStorage.getItem(key);
return item ? JSON.parse(item) : initialValue;
} catch (error) {
console.log(error);