Skip to content

Instantly share code, notes, and snippets.

View briancavalier's full-sized avatar

Brian Cavalier briancavalier

  • Pittsburgh
View GitHub Profile
@briancavalier
briancavalier / join-promises.js
Created March 30, 2011 13:17
Fan-out promise resolution
function join(promises) {
var promise = new Promise();
promise.then(
function(val) {
for (var i = 0; i < promises.length; i++) {
promises[i].resolve(val);
}
},
function(err) {
for (var i = 0; i < promises.length; i++) {
@briancavalier
briancavalier / then-promise.js
Created March 30, 2011 13:19
Promise chaining implementation of then()
Promise.prototype.then = function (onResolve, onReject, onProgress) {
if(isPromise(onResolve)) {
// Chain promise
this.then(
function(val) { onResolve.resolve(onReject ? onReject : val); },
function(err) { onResolve.reject(err); },
function(update) { onResolve.progress(update); }
);
} else {
// capture calls to then()
@briancavalier
briancavalier / chain-promises.js
Created April 6, 2011 18:03
Chain two promises such that when the first completes, the second is completed
/*
Function: chain
Chain two <Promises> such that when the first completes, the second
is completed with either the completion value of the first, or
in the case of resolve, completed with the optional resolveValue.
Parameters:
first - first <Promise>
second - <Promise> to complete when first <Promise> completes
resolveValue - optional value to use as the resolution value
@briancavalier
briancavalier / About.md
Created May 27, 2011 01:45
New wire.js prototype factory plugin coming in v0.5

Using the new wire.js prototype factory. You can use the prototype factory with any Javascript object, including object literals, just like you'd expect, since it's based on Javascript prototype chains.

This example shows it being used on an object literal, parent1, to create child1, and on a Controller (parent2) created from an AMD module, to create a specialized child2.

As you probably guessed, this will log:

Mom 1: Hello Billy
Billy 1: Hi Mom

Also check out this example of creating dojo datastores using the prototype factory.

@briancavalier
briancavalier / About.md
Created May 27, 2011 16:30
Using wire.js v0.5 prototype factory to create dojo datastores that share a query engine

Using the new wire.js prototype factory to create dojo datastores that share a query engine. This is coming in v0.5, so check it out in the dev-05 branch

// Using hitch to setup an event to be disconnected later
var cxn = events.connect(newlySelected, 'onAction', this, 'onItemAction');
this._disconnectSelected = lang.hitch(events, 'disconnect', cxn);
// As a (hideous?) one-liner:
this._disconnectSelected = lang.hitch(events, 'disconnect', events.connect(newlySelected, 'onAction', this, 'onItemAction'));
@briancavalier
briancavalier / load-images.js
Created September 27, 2011 19:11
when.any and when.some examples
// Load a collection of images and return an array of promises
function loadImages(srcs) {
// srcs = array of image src urls
// Array to hold deferred for each image being loaded
var promises = [];
// Call loadImage for each src, and push the returned deferred
// onto the deferreds array
for(var i = 0, len = srcs.length; i < len; i++) {
@briancavalier
briancavalier / universal-module.js
Created September 30, 2011 18:02 — forked from millermedeiros/universal-module.js
Universal JavaScript Module, supports AMD (RequireJS), Node.js, and the browser.
// Define a global define() that works in the current environment
(function(global) {
var define;
define = global.define;
// If AMD, just use existing global.define
if(!(typeof define === 'function' && define.amd)) {
@briancavalier
briancavalier / bp1.js
Created November 9, 2011 03:18 — forked from unscriptable/bp1.js
boilerplate for CommonJS, AMD, plain old global hackfest
(function (namespace, myLib) {
//Set up myLib here.
if (typeof define == 'function' && define.amd) {
define(myLib);
}
else {
namespace.myLib = myLib;
}