Skip to content

Instantly share code, notes, and snippets.

Paul "Joey" Clark joeytwiddle

Block or report user

Report or block joeytwiddle

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
#!/usr/bin/env bash
set -e
cache_dir="$HOME/Library/Group Containers/"
if command -v find >/dev/null && command -v mktemp >/dev/null && command -v unzip >/dev/null && command -v grep >/dev/null
then :
echo "This script requires the follow executables to run: find mktemp unzip grep"
joeytwiddle / asyncWrappers.js
Last active Jul 1, 2019
Helper to use promises in restify (might also work for express)
View asyncWrappers.js
* Use restify with promises
* Restify middleware and route handlers are passed a callback function `next`
* which must be called on success, or on an error.
* But you may prefer to define your middleware and routes using promises or
* async functions instead of using callbacks.
* To achieve that, simply wrap your middleware functions in `middleware()` and
View cooldown-monitor.js
"use strict";
Simulates an object which can heat up and overheat, but with a constant rate of cooling over time.
Useful for throttling when you want to provide an initial allowance.
For example, it could be used to manage the overheating of a weapon in a game, or to avoid errors from generating huge logfiles, but without throttling the first few errors.
joeytwiddle /
Created Nov 6, 2018
Creates a file with an ext4 filesystem inside it, and moves Dropbox into that filesystem
set -e
set -x
# You could put the fs file and the mountpoint inside your home folder
# But that may not work if you are using a fuse-mounted homefolder (e.g. encrypted with encfs)
joeytwiddle / optional.js
Last active May 2, 2019
Getting properties from an object which might be null
View optional.js
// A common situation in Javascript:
// We have a variable `obj`, and we want to get ``
// But `obj` might be null or undefined, or `` might be null or undefined
// So how can we try to get `` without risking an error?
// Here are a few different ways to do that. Which one is clearest?
joeytwiddle /
Last active Feb 26, 2019
Some things we learned in the first few months of using React

Basic React Gotchas

Note: This document was written for someone who already knows React fairly well, because it was initially a list of suggested talking points for a presentation they were planning. Ideally this document should be expanded to include clear examples for the final target audience, which is React beginners.

setState does not apply the update immediately

// Constructor
this.state = { foo: 0 };
joeytwiddle / objUtils.js
Created Jun 21, 2018
Some Array functions for Objects
View objUtils.js
// The functional way
const objUtils = {
mapProps(obj, mapFn) {
return Object.entries(obj).reduce((a, [key, value]) => (
{ ...a, [key]: mapFn(value) }
), {});
filterProps(obj, filterFn) {
return Object.entries(obj).reduce((a, [key, value]) => (
joeytwiddle /
Last active Sep 30, 2018
Do not use forEach with async-await

Do not use forEach with async-await

TLDR: Use for...of instead of forEach in asynchronous code.

The problem

Array.prototype.forEach is not designed for asynchronous code. (It was not suitable for promises, and it is not suitable for async-await.)

For example, the following forEach loop might not do what it appears to do:

joeytwiddle /
Last active Jun 23, 2018
Converting between types in Javascript

When you have a variable of one type but you want to convert it to another type, there are a few options available to you.

Here are some of the most popular methods:

|                 |                   |                         |
|    Goal         |    Short form *   |    Long form            |
|                 |                   |                         |
joeytwiddle /
Last active Jun 9, 2018
Using throw instead of Promise.reject()

I recommend using throw over return Promise.reject() because:

  1. It is shorter, which makes it easier to read.
  2. It retains some parity between synchronous and asynchronous code. (It will continue to work as expected if those lines were extracted into a synchronous function.)
  3. It can be used to break out of a stack of deeply nested synchronous function calls. (Returning a rejection only jumps up one level.)

Although throwing an error is not safe inside callback functions (which are often called from the engine, without a surrounding try-catch wrapper), it is entirely safe to throw from inside an async function (or from within a .then() handler function), because in those cases the throw will be automatically converted into a rejected promise.

Situations where it is safe to use throw

You can’t perform that action at this time.