Skip to content

Instantly share code, notes, and snippets.

Avatar

Seth Silesky silesky

View GitHub Profile
@silesky
silesky / react-loader-pattern-for-intermediate-state.js
Last active Oct 29, 2020
Loader pattern for intermediate state
View react-loader-pattern-for-intermediate-state.js
import React from "react";
import Todo from "./Todo";
import useTodosLoading from "./useTodosLoading";
import React from "react";
const Loader = ({ isLoading, errorMessage, children }) => {
if (errorMessage) return <Error errrorMessage={errorMessage} />;
if (isLoading) return <Loading />;
return children;
};
View io-ts-example-with-promises.ts
import * as t from 'io-ts';
import { PathReporter } from 'io-ts/lib/PathReporter';
import * as tPromise from 'io-ts-promise';
// [io-ts][https://github.com/gcanti/io-ts](https://github.com/gcanti/io-ts)
// [Take control of unexpected data at runtime with TypeScript](https://blog.logrocket.com/using-typescript-to-stop-unexpected-data-from-breaking-your-app/)
const fetchProduct = () => {
return Promise.resolve({
id: '123',
@silesky
silesky / GoogleMap.js
Last active Jan 12, 2018
React custom Google Maps component
View GoogleMap.js
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import { iconYouAreHere, iconPin } from 'icons';
import styles from './styles.css';
const loadScript = src => {
const ref = global.document.getElementsByTagName('script')[0];
const script = global.document.createElement('script');
script.src = src;
View multipleFilters.js
/**
* Use multiple filters on an array of object
* @param {Object[]} arr - the array to filter
* example:
* [
* {fruit: 'apple', count: 1, id: 123},
* {fruit: 'pear', count: 4, id: 234},
* {fruit: 'orange', count: 4, id: 456}
* ]
* @param {Object.<Array>} filters - an object with the filter criteria as the property names
@silesky
silesky / blog_fake_asyncify.js
Last active Oct 10, 2017
blog_fake_asyncify
View blog_fake_asyncify.js
const timer = asyncify(() => {
promiseTimeout(1000) // pseudo-code
promiseTimeout(2000)
promiseTimeout(3000)
})
@silesky
silesky / blog_async_setT2.js
Last active Oct 27, 2017
blog_async_setT2
View blog_async_setT2.js
setTimeout(() => console.log('hi'), 1000)
setTimeout(() => console.log('hi'), 2000)
setTimeout(() => console.log('hi'), 3000)
@silesky
silesky / blog_async_promisetimeout.js
Last active Oct 10, 2017
blog_async_promisetimeout
View blog_async_promisetimeout.js
const timer = asyncify(function* () {
yield promiseTimeout(1000)
yield promiseTimeout(2000)
yield promiseTimeout(3000)
})
@silesky
silesky / blog_async_final.js
Last active Oct 10, 2017
blog_async_final
View blog_async_final.js
const timer = async() => {
await promiseTimeout(1000)
await promiseTimeout(2000)
await promiseTimeout(3000)
}
View blog_fetch.js
const data = fetch(‘http://foo.com/api/') // gets skipped
console.log(data) //... [[PromiseValue]]: undefined}
View blog_settimeout.js
let x;
const reassignX = () => x = 5;
setTimeout(reassignX, 1000); // no waiting, this gets 'skipped'
console.log(x) // undefined! -- gets run before ^ is done.