Skip to content

Instantly share code, notes, and snippets.

View kriskowal's full-sized avatar

Kris Kowal kriskowal

View GitHub Profile
@kriskowal
kriskowal / LICENSE
Created May 17, 2011 17:52 — forked from 140bytes/LICENSE.txt
Nano-promises
Copyright 2011 Kristopher Michael Kowal. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
var q = require('q');
var a = q.defer();
var b = q.defer();
q.when(a, function() {
console.log('calling b');
return q.when(b, function() {
console.log('throwing');
throw new Error('Hello World');
@kriskowal
kriskowal / spread.js
Created December 27, 2011 23:11 — forked from mattkime/spread.js
added spread test
"use strict";
var Q = require("../q");
exports['test spread'] = function (ASSERT, done) {
Q.ref([1,2,3])
.spread(function (a, b, c) {
ASSERT.equal(a, 1, 'spread 1');
ASSERT.equal(b, 2, 'spread 2');
ASSERT.equal(c, 3, 'spread 3');
@kriskowal
kriskowal / streams.js
Created March 19, 2012 20:02 — forked from domenic/streams.js
Streams are painful
function promiseResponse(request) {
var deferred = Q.defer();
request.on("error", deferred.reject);
request.on("response", deferred.resolve);
return deferred.promise
// if you must:
.fin(function () {
request.removeListener("error", deferred.reject);
request.removeListener("response", deferred.resolve);
});
@kriskowal
kriskowal / streams.js
Created April 9, 2012 18:06 — forked from domenic/streams.js
Streams are painful
var IO = require("q-io");
function exists(id) {
var deferred = Q.defer();
var request = knoxClient.get(id);
request.on("error", deferred.reject);
request.on("response", function (response) {
if (response.statusCode === 200)
deferred.resolve(IO.Reader(response));
} else {
@kriskowal
kriskowal / huffman.py
Created July 24, 2012 16:32 — forked from ryanwitt/huffman.py
silly huffman coding example
__all__ = (
'build',
'endode',
'decode',
)
phrase = 'A man a plan a canal Panama'.lower()
def build(phrase):
"""
@kriskowal
kriskowal / README.md
Created August 23, 2012 02:29 — forked from aaronj1335/README.md
bootstrapping mr
We couldn’t find that file to show.

HORSES AND PONIES and WAGONS n CARTS:

By Deb White, Seattle, [NWRinger]

Can horses swim the Brandywine?

The Fellowship of the Ring, 112 the question of whether or not horses can cross the Brandywine River comes up at Buckleberry Ferry. We know that at least 1 hobbit pony can ride the ferry, as Merry takes his across on it when he searches for Frodo who is late to arrive; but there doesn’t seem to be an ability for them, either ponies or horses, to swim it at this point, they must go up to the Bridge or down to Sarn Ford, as Merry’s response to Frodo is: “Can horses cross the river?”

“They can go twenty miles north to Brandywine Bridge - or they might swim,” answered Merry, “Though I never heard of any horse swimming the Brandywine.”

var DIR = '/tmp/test',
FILE_PATTERN = /test/,
fs = require('q-io/fs');
fs.list(DIR)
.then(function (files) {
var matched = files.filter(function (item) {
return item.match(FILE_PATTERN);
});
if (matched.length !== 1) {
@kriskowal
kriskowal / index.js
Last active January 7, 2016 22:55 — forked from brianleroux/index.js
callback-with-promise
const fs = require('fs')
function readPackage(callback) {
return fs.readFile('./package.json').nodeify(callback);
}
module.exports.readPackage = readPackage