Skip to content

Instantly share code, notes, and snippets.

async function sleep(ms) {
return new Promise(r => setTimeout(r, ms))
}
async function foo() { console.log('s foo'); await sleep(2000); console.log('d foo'); return 'foo'; }
async function bar() { console.log('s bar'); await sleep(1000); console.log('d bar'); return 'bar'; }
async function baz() { console.log('s baz'); await sleep(2000); console.log('d baz'); return 'baz'; }
async function run() {
console.log('start')
@danharper
danharper / ngBridge.js
Last active August 29, 2015 14:16
ngBridge - wrap your Angular modules to support ES6 classes, and stricter assurances of DI
export function ngBridged(name, deps) {
return ngBridge(angular.module(name, deps))
}
export function ngBridge(ngModule) {
const register = (type, ...args) => ngBridge(ngModule[type](...args))
const injectable = type => (name, injectableFunc) => register(type, name, ngInject(injectableFunc))
const pass = type => (...args) => register(type, ...args)
export default class SayingHelloComponent {
static ngInject() {
return [] // register your dependencies here (Angular 1 style, with string names)
}
static ddo() {
return {
bind: {
person: '@'
},
name: 'sayingHello', // component's name
@danharper
danharper / ngDirective.js
Created March 6, 2015 11:44
ngDirective - note I haven't exactly dove deep into directives, so I'm sure there are other forms this doesn't account for (e.g. only having the link method?)
import ngInject from './ngInject'
export default function ngDirective(directive) {
let func = function(...injectedArgs) {
let link = (...directiveArgs) => new directive(...injectedArgs, ...directiveArgs)
return {...directive.ddo(), link}
}
func.$inject = ngInject(directive).$inject
@danharper
danharper / ngInject.js
Created March 5, 2015 21:58
ngInject - define injections as a static method in your class
// ngInject.js
function ngInject(func) {
if (func.ngInject)) {
func.$inject = func.ngInject()
}
return func
}
function sleep(ms = 0) {
return new Promise(r => setTimeout(r, ms))
}
async function run() {
await sleep(2000)
console.log('2s later')
}
run()
@danharper
danharper / gulpfile.js
Last active September 25, 2024 09:04
New ES6 project with Babel, Browserify & Gulp
var gulp = require('gulp');
var sourcemaps = require('gulp-sourcemaps');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var browserify = require('browserify');
var watchify = require('watchify');
var babel = require('babelify');
function compile(watch) {
var bundler = watchify(browserify('./src/index.js', { debug: true }).transform(babel));
@danharper
danharper / CancellationTokenSource.js
Last active February 7, 2026 17:47
JavaScript "CancellationToken" for cancelling Async/Promise functions
const CANCEL = Symbol();
class CancellationToken {
constructor() {
this.cancelled = false;
}
throwIfCancelled() {
if (this.isCancelled()) {
@danharper
danharper / TimeZones.php
Last active July 5, 2016 22:10
Generates a list of all timezone identifiers, with their offset & made (slightly) more human-readable
<?php
class TimeZones {
/**
* @return array
*/
public function generate()
{
$identifiers = DateTimeZone::listIdentifiers();
@danharper
danharper / circle.yml
Created February 11, 2015 12:46
How we deploy InventoryBase from CircleCI
machine:
php:
version: 5.6.5
dependencies:
cache_directories:
- vendor
- node_modules
pre:
- sudo pip install awscli