Skip to content

Instantly share code, notes, and snippets.

Avatar

Damian Janowski djanowski

View GitHub Profile
@djanowski
djanowski / throttle_async.js
Created Feb 1, 2019
Throttle an async (promise-returning) function in JavaScript/Node.js
View throttle_async.js
// Throttles an async function.
function throttleAsync(fn, wait) {
let lastRun = 0;
let currentRun = null;
async function throttled(...args) {
if (currentRun)
return currentRun;
const currentWait = lastRun + wait - Date.now();
@djanowski
djanowski / each_async_update.js
Created Nov 29, 2017
Concurrently transform a MongoDB cursor into a stream of bulk MongoDB updates.
View each_async_update.js
// Concurrently transform a MongoDB cursor into a stream of bulk MongoDB updates.
//
// Usage:
//
// const users = db.collection('users');
// const usersToUpdate = users.find({ email: null });
//
// await eachAsyncUpdate(users, usersToUpdate, async function(user) {
// const userEmail = await findUserEmail(user);
//
@djanowski
djanowski / timezones.txt
Created Nov 29, 2017
Good-enough default IANA time zones by US state
View timezones.txt
AL America/Chicago
AK America/Anchorage
AS Pacific/Samoa
AZ America/Denver
AR America/Chicago
CA America/Los_Angeles
CO America/Denver
CT America/New_York
DE America/New_York
DC America/New_York
View autotest
#!/usr/bin/env bash
# Monitors your tree and runs tests when anything changes.
#
# `make`, `yarn test`, etc. automatically detected.
#
# $ autotest
#
# Run a specific command on changes:
#
View autotest
#!/usr/bin/env bash
# Monitors your tree and runs tests when anything changes.
#
# `make`, `yarn test`, etc. automatically detected.
#
# $ autotest
#
# Run a specific command on changes:
#
View autotest
#!/usr/bin/env bash
# Monitors your tree and runs tests when anything changes.
#
# `make`, `yarn test`, etc. automatically detected.
#
# $ autotest
#
# Run a specific command on changes:
#
View mongoose_text_index.js
const Mongoose = require('mongoose');
Mongoose.Promise = Promise;
Mongoose.connect(`mongodb://localhost/test-${Date.now()}`);
const PersonSchema = new Mongoose.Schema({
name: {
first: { type: String },
last: { type: String }
}
View nnm
#!/usr/bin/env bash
# `nvm use` the version specified in package.json.
source $(brew --prefix nvm)/nvm.sh
set -eo pipefail
die() {
echo "$@" >&2
@djanowski
djanowski / autotest
Last active Feb 1, 2016
ag -l + entr = autotest!
View autotest
#!/usr/bin/env bash
# Monitors your tree and runs tests when anything changes.
#
# Run `make`, `npm test`, etc. automatically detected.
#
# $ autotest
#
# Run a specific command on changes:
#
@djanowski
djanowski / .000-README
Last active Jan 25, 2016
Babel bug with `this` inside async arrow function - npm i && npm test
View .000-README
babel-preset-stage-2 pulls in the newest version of babel-plugin-transform-async-to-generator,
however the async arrow function still has `this` = `undefined`.
If I manually install babel-plugin-transform-async-to-generator and add it to the list of plugins
in .babelrc, everything works as expected.
Steps to reproduce:
$ npm install && npm test
You can’t perform that action at this time.