Skip to content

Instantly share code, notes, and snippets.

Avatar

Michael Pearson mipearson

View GitHub Profile
View async_js_intro.md

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 Sep 23, 2019
Demonstration of handling asynchronous functions in loops in typescript using async/await
View typescriptAsyncLoops.ts
// 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 Dec 11, 2018
Grabbing a list of which Buildkite pipelines use which agent queues
View buildkite_which_queue.md

Run this query via the graph explorer:

{
  organization(slug: "your-org-slug") {
    pipelines(first: 500) {
      edges {
        node {
          name
@mipearson
mipearson / index.ts
Created Aug 19, 2018
Demonstration of using AWS CDK
View index.ts
/*
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 Jul 5, 2018
Structured Logging with Rails & ELK example files
View elk.rb
# 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
View development_view_resolve_caching.rb
# 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
View capybara_timers.rb
# 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
View scrabble_benchmarking.rb
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
}
View scrabble_benchmarking.rb
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
}
@mipearson
mipearson / lib__npm_check.rb
Last active Sep 1, 2016
Simple NPM version checker
View lib__npm_check.rb
class NpmCheck
# Checks package.json against the local node_modules directory
# to assert that every package specified in package.json is
# present in node_modules. If possible, it performs a basic
# "newer or equal than" check on the package's version. Without
# access to npm's semver primitizes & parsing it is a bit more difficult
# to perform proper version matching checks.
#
# Requires https://github.com/jlindsey/semantic to parse versions.
#
You can’t perform that action at this time.