Skip to content

Instantly share code, notes, and snippets.

@mipearson
mipearson / mtgtabsbackup.md
Last active August 15, 2021 02:54
Mountain Goats tabs index backup

Asynchronous Programming

Javascript code generally handles network & IO operations asynchronously: instead of blocking on a file read or database call, a callback is called when the data is ready.

There are three main ways of handling this in Javascript (and therefore Typescript) programs: callbacks, promises, and async/await.

TL;DR

Use async/await.

@mipearson
mipearson / typescriptAsyncLoops.ts
Created September 23, 2019 01:05
Demonstration of handling asynchronous functions in loops in typescript using async/await
// Demonstration of different ways of running asynchronous functions in loops
import util = require("util");
const arr = ["a", "b", "c", "d"];
const setTimeoutPromise = util.promisify(setTimeout);
// Asynchronously wait a very short amount of time and then print our value
async function waitAndPrint(s: string): Promise<void> {
await setTimeoutPromise(Math.random() * 100);
console.log(s);
@mipearson
mipearson / buildkite_which_queue.md
Created December 11, 2018 05:11
Grabbing a list of which Buildkite pipelines use which agent queues

Run this query via the graph explorer:

{
  organization(slug: "your-org-slug") {
    pipelines(first: 500) {
      edges {
        node {
 name
@mipearson
mipearson / index.ts
Created August 19, 2018 08:16
Demonstration of using AWS CDK
/*
I was curious as to whether it was possible to make resources
created using CDK constructs work with raw CloudFormation
resources and vice versa.
This aws-cdk app demonstrates a VPC and an ELB that belongs to it
in different, dependant stacks, leveraging CFN outputs / imports,
and leveraging CDK's method of passing information from one
stack to another.
*/
@mipearson
mipearson / elk.rb
Created July 5, 2018 06:07
Structured Logging with Rails & ELK example files
# Copyright 2018 Marketplacer. MIT License.
require 'json'
require 'active_support/core_ext/class/attribute'
require 'active_support/log_subscriber'
module Elk
class RequestLogSubscriber < ActiveSupport::LogSubscriber
PAYLOAD_KEYS_TO_COPY = %i{
controller
# include this mixin (or put the code directly) into your application_controller.rb
module Shared
module DevelopmentViewResolveCaching
extend ActiveSupport::Concern
# Speeds up development view rendering by about ~300ms as we don't need
# to call the slow view resolver more than once per partial.
#
# From https://github.com/rails/rails/issues/20752
# Copyright 2018 Marketplacer under the MIT license.
class CapybaraTimers
class << self
attr_accessor :nesting, :total_time
def setup
# Wrap every DSL method and every matcher with our timing function
Capybara::Session::DSL_METHODS.each do |name|
setup_timer_on_method(Capybara::DSL, name)
end
POINTS = {
"A" => 1, "B" => 3, "C" => 3, "D" => 2,
"E" => 1, "F" => 4, "G" => 2, "H" => 4,
"I" => 1, "J" => 8, "K" => 5, "L" => 1,
"M" => 3, "N" => 1, "O" => 1, "P" => 3,
"Q" => 10, "R" => 1, "S" => 1, "T" => 1,
"U" => 1, "V" => 4, "W" => 4, "X" => 8,
"Y" => 4, "Z" => 10
}
POINTS = {
"A" => 1, "B" => 3, "C" => 3, "D" => 2,
"E" => 1, "F" => 4, "G" => 2, "H" => 4,
"I" => 1, "J" => 8, "K" => 5, "L" => 1,
"M" => 3, "N" => 1, "O" => 1, "P" => 3,
"Q" => 10, "R" => 1, "S" => 1, "T" => 1,
"U" => 1, "V" => 4, "W" => 4, "X" => 8,
"Y" => 4, "Z" => 10
}