Skip to content

Instantly share code, notes, and snippets.

Avatar

Kris Kowal kriskowal

View GitHub Profile
@kriskowal
kriskowal / gist:8833212
Created Feb 5, 2014
This is why we can’t have nice things.
View gist:8833212
> 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 Aug 29, 2015
Promise Inspector Protocol
View promise-inspector-protocol.md

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

View asap-bench.js
var rawAsap = require("./raw");
var asap = require("./asap");
suite("yield", function () {
bench("raw asap", function (done) {
rawAsap(done);
});
bench("asap", function (done) {
asap(done);
View mutex.js
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);
View herd.js
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 () {
@kriskowal
kriskowal / memory.asm
Created Feb 27, 2014
No, this is the best version of the memory game.
View memory.asm
;
; CS 30 Assembly
; Prof. R. Farrel
; Kris Kowal
; Programming Assignment #3
;
; Memory
; by Kris Kowal
; 2001-10-04-1604 PDT
;
View idea-test.js
describe("", function () {
it("", function () {
var frame = window.open("blah.html?id=10");
return inFrame(frame, function () {
expect(window.location).toEndWith("id=10");
});
});
});
View child.js
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);
}
});
View retry.js
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);
});
}
});
View fs.js
var nodeFs = require("fs");
var fs = {};
fs.readFile = function (path, callback) {
nodeFs.readFile(path, callback);
};
fs.readChunk = function (path, start, end, callback) {
You can’t perform that action at this time.