Skip to content

Instantly share code, notes, and snippets.

View kriskowal's full-sized avatar

Kris Kowal kriskowal

View GitHub Profile
@kriskowal
kriskowal / gist:8833212
Created February 5, 2014 21:12
This is why we can’t have nice things.
> function fool(a, b, c) {
....... trustme(arguments);
....... console.log(a, b, c);
....... }
undefined
> function trustme(args) {
....... args[0] = 10;
....... args[1] = 11;
....... args[2] = 30;
....... }
@kriskowal
kriskowal / promise-inspector-protocol.md
Last active August 29, 2015 13:56
Promise Inspector Protocol

One of the features of promises, in the Promises/A+ sense, is that they capture thrown exceptions so they can be handled asynchronously. This has the negative consequence that it is possible for an error to be silently ignored forever. There is a long list of measures that can mitigate this problem, but the real solution is to use a Promise Inspector.

The JavaScript console is not satisfying. The trouble with a rejected promise is that it might not be later connected to a promise chain that would handle recover from or surface the issue. The naïve solution would be to log every exception to the console, but then it becomes difficult to distinguish errors that have been handled programmatically from errors that never will be. Ideally, the promise inspector would show rejected promises until they are handled and then hide them. That way, if a program reaches an obviously stable or idle state and a rejection remains on the console, it is very likely due to a programmer’s error.

There is a less obvious class of

var rawAsap = require("./raw");
var asap = require("./asap");
suite("yield", function () {
bench("raw asap", function (done) {
rawAsap(done);
});
bench("asap", function (done) {
asap(done);
var map = require("collections/map");
function protectAgainstThunderingHerd(method) {
var memo = new Map();
return function protected() {
var self = this;
var args = Array.prototype.slice.call(arguments);
if (!memo.has(args)) {
memo.set(Q.try(function () {
return method.apply(this, args);
}).finally(function () {
var Qeueue = require("q/queue");
var semaphore = new Queue();
semaphore.put(); // once for one job at a time
function exclusive(method) {
return function wrapped() {
var self = this, args = Array.prototype.slice.call(arguments);
return semaphore.get()
.then(function () {
return method.apply(self, args);
@kriskowal
kriskowal / memory.asm
Created February 27, 2014 18:22
No, this is the best version of the memory game.
;
; CS 30 Assembly
; Prof. R. Farrel
; Kris Kowal
; Programming Assignment #3
;
; Memory
; by Kris Kowal
; 2001-10-04-1604 PDT
;
describe("", function () {
it("", function () {
var frame = window.open("blah.html?id=10");
return inFrame(frame, function () {
expect(window.location).toEndWith("id=10");
});
});
});
var Connection = require("./q-connection");
var ProcessStream = require("q-io/node/process-stream");
var stream = ProcessStream(process);
var remote = Connection(stream, {
exit: function (code) {
process.exit(code);
}
});
function retry(f, ms, times) {
return Q.fcall(f)
.then(null, function (error) {
if (times > 0) {
return Q.delay(ms)
.then(function () {
return retry(f, ms * 2, times - 1);
});
}
});
var nodeFs = require("fs");
var fs = {};
fs.readFile = function (path, callback) {
nodeFs.readFile(path, callback);
};
fs.readChunk = function (path, start, end, callback) {