Skip to content

Instantly share code, notes, and snippets.

Brian Cavalier briancavalier

Block or report user

Report or block briancavalier

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
briancavalier /
Created Mar 21, 2014
when 3.0 es6-shim results for Safari 7 OS X Mavericks
briancavalier / 1-test.js
Last active Aug 29, 2015
Thinking about promise error heuristics
View 1-test.js
var Promise = require('../../when').Promise;
Promise.resolve().then(function outer() {
return Promise.resolve().then(function inner() {
return Promise.resolve().then(function evenMoreInner() {
briancavalier / fold.js
Created Mar 26, 2014
Simple promise.fold usage example
View fold.js
// Should log:
// 3
// Bob
// b
var when = require('when');
when.resolve(1).fold(sum, 2).done(console.log);
when.resolve({ name: 'Bob' }).fold(get, 'name').done(console.log);
briancavalier / 1-1-test.js
Last active Aug 29, 2015
Seriously detailed stacks in when.js 3.1
View 1-1-test.js
var Promise = require('../../when').Promise;
function error(){
throw new Error('error');
return Promise.resolve(123);
View 1-cycle.js
var when = require('when');
// Creates a cycle p1 -> p2 -> p3 -> p4 -> p1
var p1 = when.promise(function(r1) {
var p2 = when.promise(function(r2) {
var p3 = when.promise(function(r3) {
var p4 = when.promise(function(r4) {
setTimeout(function() {
r4(p1); // Cycle! resolve p4 with p1
// Before fold-related changes
Handler.prototype.when(resolve, notify, context, receiver, onFulfill, onReject, onProgress);


  • resolve - continuation function called by the promise machinery (technically by FulfilledHandler or RejectedHandler) with the result of either onFulfilled or onRejected. Sends values or errors on to the next handler in the chain.
  • notify - continuation function called with the result of onProgress. Sends progress values on to the next handler in the chain.
  • context - thisArg that will be used when calling resolve and/or notify
  • receiver - thisArg that will be used when calling onFulfill, onReject, or onProgress
briancavalier / redis-1.js
Last active Aug 29, 2015
RedisClient and when/node.liftAll
View redis-1.js
// Lift only the RedisClient prototype command methods
// to a new prototype so you can create many instances
var node = require('when/node');
var net = require('net');
var RedisClient = require('redis').RedisClient;
var host = '';
var port = 6379;
var commands = require('redis/lib/commands');
function liftCommands(proto, f, n) {
briancavalier / create-prototype.js
Last active Aug 29, 2015
Use create to beget from an existing object
View create-prototype.js
// Create a base instance
base: {
create: {
module: 'controls/switch/switchControl',
args: {
name: 'switcher'
properties: {
briancavalier / 1-cycle.js
Last active Aug 29, 2015
Upcoming when.js 3.2.0 will detect many arbitrary-length graph cycles (with no perf penalty. try it in the dev branch)
View 1-cycle.js
var when = require('when');
var d1 = when.defer();
var d2 = when.defer();
var d3 = when.defer();
View gist:becf2e7d7d6726730aa8
var deferred = {};
var keys = require('when/keys');
if (data.picture) {
deferred.picture = webservices.image(data['picture']);
if (data.screen) {
deferred.screen = webservices.image(data['screen']);
You can’t perform that action at this time.