Skip to content

Instantly share code, notes, and snippets.

View briancavalier's full-sized avatar

Brian Cavalier briancavalier

  • Pittsburgh
View GitHub Profile
@briancavalier
briancavalier / 1-intro.md
Created March 21, 2014 01:49
when 3.0 es6-shim results for Safari 7 OS X Mavericks https://github.com/stefanpenner/promise_benchmarks
@briancavalier
briancavalier / 1-test.js
Last active August 29, 2015 13:57
Thinking about promise error heuristics
require('../monitor/console');
var Promise = require('../../when').Promise;
Promise.resolve().then(function outer() {
return Promise.resolve().then(function inner() {
return Promise.resolve().then(function evenMoreInner() {
oops();
});
});
});
@briancavalier
briancavalier / fold.js
Created March 26, 2014 19:20
Simple promise.fold usage example
// 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
briancavalier / 1-1-test.js
Last active August 29, 2015 13:57
Seriously detailed stacks in when.js 3.1
require('../../monitor/console');
var Promise = require('../../when').Promise;
function error(){
throw new Error('error');
}
Promise.resolve('foo').then(function(){
return Promise.resolve(123);
}).then(function(){
require('when/monitor/console');
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);

Params:

  • 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
briancavalier / redis-1.js
Last active August 29, 2015 13:58
RedisClient and when/node.liftAll
// 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 = '127.0.0.1';
var port = 6379;
var commands = require('redis/lib/commands');
function liftCommands(proto, f, n) {
@briancavalier
briancavalier / create-prototype.js
Last active August 29, 2015 13:58
Use create to beget from an existing object
{
// Create a base instance
base: {
create: {
module: 'controls/switch/switchControl',
args: {
name: 'switcher'
}
},
properties: {
@briancavalier
briancavalier / 1-cycle.js
Last active August 29, 2015 14:00
Upcoming when.js 3.2.0 will detect many arbitrary-length graph cycles (with no perf penalty. try it in the dev branch)
var when = require('when');
var d1 = when.defer();
var d2 = when.defer();
var d3 = when.defer();
d1.resolve(d2.promise);
d2.resolve(d3.promise);
d3.resolve(d1.promise);
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']);
}