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
View when-failure.js
var when1 = require('when1');
var when2 = require('when2');
when1.Promise.onPotentiallyUnhandledRejectionHandled = function(r) {
console.log('when1 Handled', r);
var rejectedPromise = when1.reject(new Error('Intentional failure'));
when2(rejectedPromise).done(undefined, function(error){
console.log('Rejection properly handled:', error);
View timer-collision.js
'=>test': function(done) {
this.clock = this.useFakeTimers();
setTimeout(done, 1000);
View flat.js
return mopidy.tracklist.add(playlist.tracks).then(function (tlTracks) {
}).then(function() {
return mopidy.playback.getCurrentTrack().then(function (track) {
console.log("Now playing:", trackDesc(track));
briancavalier / find-first.js
Created Jul 2, 2014
Simplified find left-most fulfilled
View find-first.js
function findFirst(promises){
return recurseFindFirst(0, [], promises);
function recurseFindFirst(i, errors, promises) {
if(i === promises.length) {
var e = new Error('All promises rejected');
e.errors = errors;
return when.reject(e);
View splice.js
// JSON Patch containing a splice operation with context
"op": "splice",
"path": "/3",
"+": ["d", "e"],
"-": ["x", "y", "z"],
"<": ["a", "b", "c"],
">": ["f", "g"]
View mapWithKeys.js
var keys = require('when/keys');
function mapWithKeys(object, f) {
return keys.all(Object.keys(object).reduce(function(o, k) {
o[k] = when(object[k], function(value) {
return f(value, k);
return o;
}, {}));
View curry.js
module.exports = curry;
function curry (f) {
var arity = f.length;
var params = [];
var end = createEnd(f, arity);
return createCurried(params, arity, end);
function createEnd (f, arity) {

This is a simple micro-benchmark of four ways to curry JavaScript functions. The benchmark focuses on measuring the performance of the resulting curried functions, not on the performance of currying the functions in the first place. That'd also be an interesting comparison, especially since two of these approaches rely on dynamic compilation at curry-time. Typically, though, functions should be curried once at load/init/startup time, so the runtime performance of the resulting curried functions is typically more important to the overall application.

Note: I'm not a micro-benchmarking expert, so I might be doing something dumb! If you see a problem, please suggest a fix :)


unscriptable dynamic compile

This one was taken from @unscriptable's gist that uses dynamic compilation to generate the final function in the curry chain.

View wrap-callback.js
var when = require('when');
when.promise(function(resolve) {
setTimeout(resolve, 100);
}).then(function() {
return when.promise(function(resolve) {
setTimeout(resolve, 100);
}).then(function() {
return when.promise(function(resolve) {
You can’t perform that action at this time.